Spring MVC 3.0讲解
来源:互联网 发布:文章搜索软件 编辑:程序博客网 时间:2024/06/01 17:11
Spring MVC 3.0 深入
核心原理
1.
2.
3.
4.
5.
6.
7.
spring3.0中相关jar包的含义
org.springframework.aop-3.0.3.RELEASE.jar
spring的aop面向切面编程
org.springframework.asm-3.0.3.RELEASE.jar
spring独立的asm字节码生成程序
org.springframework.beans-3.0.3.RELEASE.jar
IOC的基础实现
org.springframework.context-3.0.3.RELEASE.jar
IOC基础上的扩展服务
org.springframework.core-3.0.3.RELEASE.jar
spring的核心包
org.springframework.expression-3.0.3.RELEASE.jar
spring的表达式语言
org.springframework.web-3.0.3.RELEASE.jar
web工具包
org.springframework.web.servlet-3.0.3.RELEASE.jar
mvc工具包
@Controller控制器定义
和Struts1一样,Spring的Controller是Singleton的。这就意味着会被多个请求线程共享。因此,我们将控制器设计成无状态类。
在spring 3.0中,通过@controller标注即可将class定义为一个controller类。为使spring能找到定义为controller的bean,需要在spring-context配置文件中增加如下定义:
base-package="com.sxt.web"/>
@RequestMapping
package com.sxt.web;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.sxt.service.UserService;
@Controller
@RequestMapping("/user.do")
public class UserController
}
@RequestMapping(params="method=reg5")
@SessionAttributes
@Controller
@RequestMapping("/user.do")
@SessionAttributes({"u","a"})
public class UserController
}
@ModelAttribute
package com.sxt.web;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@RequestMapping("/user.do")
@SessionAttributes({"u","a"})
public class UserController
public String reg5(@ModelAttribute("u")String uname[1]
}
先调用reg4方法,再调用reg5方法。我们发现控制台打印出来:尚学堂高淇
Controller类中方法参数的处理
Cont
1.
a)
b)
@RequestMapping(params="method=reg4")
前缀为:/WEB-INF/jsp/
在转发到:/WEB-INF/jsp/index.jsp
2.
请求转发和重定向
package com.sxt.web;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@RequestMapping("/user.do")
public class UserController
//
//
//
}
获得request对象、session对象
普通的Controller类,示例代码如下:
@Controller
@RequestMapping("/user.do")
public class UserController
}
ModelMap
package com.sxt.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
@Controller
@RequestMapping("/user.do")
public class UserController extends MultiActionController
}