SpringMVC之基于注解的MVC实现
1 示例1
1.1 spring-mvc.xml文件
添加DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter等相关信息。其中
DefaultAnnotationHandlerMapping:支持通过url找到相关的action
AnnotationMethodHandlerAdapter:支持通过url匹配action定义方法
base-package:定义扫描的范围,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component@Controller@Service等这些注解的类,则把这些类注册为bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
<!--
DefaultAnnotationHandlerMapping:支持通过url找到相关的action
AnnotationMethodHandlerAdapter:支持通过url匹配action定义方法
base-package:定 义扫描的范围,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean
-->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<context:component-scan base-package="*"></context:component-scan>
</beans>
1.2 控制类
加入@Controller,@RequestMapping注解信息
@Controller //用来声明控制器
@RequestMapping("/student")
public class StudentAction {
public StudentAction(){
System.out.println("---StudentAction构造方法被调用---");
}
//访问可用student/save.action,save后边的action是根据web.xml配置来的
//如果要添加其它的数据到最后跳转过去的页面,可以在方法中添加ModelMap的参数,例如 : public String save(Student student,ModelMap map){
//...,通过map再存放其它的数据
@RequestMapping(value="/save")
public ModelAndView save(Student student){
System.out.println("save方法注入的student对象:"+student);
System.out.println("---调用业务逻辑进行业务处理---");
//修改学生名字,跳转到下一页面时看能否显示修改后的名字
student.setStuName("rename");
//直接使用字符串,返回视图,进行结果展现等
return new ModelAndView("forward:/jsp/main.jsp");
}
//同一个action中可以定义多个方法,方法的返回类型也可以用String
@RequestMapping(value="/update")
public String update(Student student,ModelMap paramMap){
System.out.println("update方法已注入student对象:"+student);
System.out.println("---调用业务逻辑进行业务处理---");
paramMap.put("other","testOtherValue");
//直接使用字符串,返回视图,进行结果展现等
return "forward:/jsp/main.jsp";
}
}
2 示例2(基于annotation-driven的注解)
基于上面的示例,在spring3中可以进一步简化配置,取代上面的注解方式.
步骤如下:
1.使用上面的action类,仍然给类及方法添加@Controller(类)、@RequestMapping(类及方法)注解
2.本文件顶部添加spring mvc 命名空间的信息(可以参考org.springframework.web.servlet.config包)
3.添加下面注解驱动<mvc:annotation-driven>,取代了上面的DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter,并启动了json的注解
2.1 spring-mvc.xml文件, 红色部分
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
">
<!--
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<context:component-scan base-package="*"></context:component-scan>
-->
<!-- mvc:annotation-driven,取代了上面的DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
两个Bean的配置 -->
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="*"/>
</beans>
3 @SessionAttributes与model.addAttribute使用
3.1 modelMap属性注入到Session
如果希望在多个请求中共享 ModelMap 中的属性,必须将其属性转存到 session 中,这样 ModelMap 的属性才可以被跨请求访问;
@Controller
@RequestMapping("/student")
//下边如有多个属性可以用 @SessionAttributes({“attr1”,”attr2”})。
@SessionAttributes("user")
public class StudentAction {
public StudentAction(){
System.out.println("---StudentAction构造方法被调用---");
}
@RequestMapping(value="/save")
public String save(Student student,ModelMap map){
System.out.println("---调用业务逻辑进行业务处理---");
Student s2=new Student();
s2.setStuAge(11);
s2.setStuId(11111);
map.addAttribute("user", s2);//属性名必须与session一致
//map.addAttribute("stu", student);
//直接使用字符串,返回视图,进行结果展现等
return "forward:/jsp/main.jsp";
}
}
3.2 session属性注入到ModelMap
在参数中使用@ModelAttribute("user"),可以获取@SessionAttributes("user")值
@Controller
@RequestMapping("/student")
//下边如有多个属性可以用 @SessionAttributes({“attr1”,”attr2”})。
@SessionAttributes("user")
public class StudentAction {
public StudentAction(){
System.out.println("---StudentAction构造方法被调用---");
}
@RequestMapping(value="/save")
public String save(Student student,ModelMap map){
System.out.println("---调用业务逻辑进行业务处理---");
Student s2=new Student();
s2.setStuAge(11);
s2.setStuId(11111);
s2.setStuName("testname");
map.addAttribute("user", s2);
//map.addAttribute("stu", student);
//直接使用字符串,返回视图,进行结果展现等
return "forward:/jsp/main.jsp";
}
//同一个action中可以定义多个方法
@RequestMapping(value="/update")
public String update(@ModelAttribute("user")Student student){
System.out.println("update方法已注入student对象:"+student);
System.out.println("---调用业务逻辑进行业务处理---");
//直接使用字符串,返回视图,进行结果展现等
return "forward:/jsp/main.jsp";
}
}