本例子为你讲解在spring3中如何使用基于注解的mvc框架.
例子中使用到的工具:
MyEclipse 9.1
jdk 1.6
1.添加Jar包引用
由于使用了Maven管理项目,所以,第一步就是添加引用.(没有使用Maven怎么办?那你直接下载Spring3的压缩包,添加相应的Jar文件就可以了。)
Java代码
2.编写 Controller 和 Mapping
我们采用注解的方式配置,如果想使用XML的方式,可以查看文档,都是一样的配制方法.
Java代码
package com.vito.action;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/welcome")
public class HelloWorldController {
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
}
3.JSP视图
Java代码
Message : ${message}
4.Spring配置文件
Java代码
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
5.web.xml
Java代码
"1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
6.成果
访问:×××/technology
就可以得到这样的画面了: 
本站所有代码来源请查看:×××/technology