Spring 向页面传值以及接受页面传过来的参数的方式
一、 从页面接收参数
Spring MVC接收请求提交的参数值的几种方法:
1.使用HttpServletRequest获取。
@RequestMapping("/login.do")
public String login(HttpServletRequest request){
String name = request.getParameter("name")
String pass = request.getParameter("pass")
}
2.使用@RequestParam注解。
@RequestMapping("/login.do")
public String login(HttpServletRequest request, String name, @RequestParam("pass") String password) // 表单属性是pass,用变量password接收
{
syso(name);
syso(password)
}
3.使用自动机制封装成实体参数。
<form action="login.do">
用户名:<input name="name"/>
密码:<input name="pass"/>
<input type="submit" value="登陆">
</form>
//封装的User类
public class User{
private String name;
private String pass;
}
```
```
@RequestMapping("/login.do")
public String login(User user)
{
syso(user.getName());
syso(user.getPass());
}
```
二、 向页面传值
当Controller组件处理后,需要向响应JSP传值时,可以使用的方法:
1.使用HttpServletRequest 和 Session 然后setAttribute(),就和Servlet中一样
Model数据会利用HttpServletRequest的Attribute传值到success.jsp中
```
@RequestMapping("/login.do")
public ModelAndView login(String name,String pass){
User user = userService.login(name,pwd);
Map<String,Object> data = new HashMap<String,Object>();
data.put("user",user);
return new ModelAndView("success",data);
}
```
2.使用ModelAndView对象
3.使用ModelMap对象
使用ModelMap参数对象示例:
ModelMap数据会利用HttpServletRequest的Attribute传值到success.jsp中
```
@RequestMapping("/login.do")
public String login(String name,String pass ,ModelMap model){
User user = userService.login(name,pwd);
model.addAttribute("user",user);
model.put("name",name);
return "success";
}
```
4.使用@ModelAttribute注解
在Controller方法的参数部分或Bean属性方法上使用
@ModelAttribute数据会利用HttpServletRequest的Attribute传值到success.jsp中
```
@RequestMapping("/login.do")
public String login(@ModelAttribute("user") User user){
//TODO
return "success";
}
@ModelAttribute("name")
public String getName(){
return name;
}
```
5.Session存储:可以利用HttpServletRequest的getSession()方法
```
@RequestMapping("/login.do")
public String login(String name,String pwd,ModelMap model,HttpServletRequest request){
User user = serService.login(name,pwd);
HttpSession session = request.getSession();
session.setAttribute("user",user);
model.addAttribute("user",user);
return "success";
}
```
6.自定义Map
```
@ResponseBody
@RequestMapping(value = "/updatestatus", method = RequestMethod.POST)
public Map<String, Object> updateStatus(HttpServletRequest request) {
Map<String, Object> result = new HashMap<String, Object>();
String id = request.getParameter("id");
SystemAdministrator sysadmin=systemAdminBiz.get(Integer.valueOf(id));
sysadmin.setStatus(1);
boolean flag = systemAdminBiz.update(sysadmin);
result.put("status", flag);
return result;
}
```
```
@RequestMapping(value = "/list", method = {RequestMethod.POST,RequestMethod.GET})
public String queryAdministrator(HttpServletRequest request,ModelMap model) {
Integer roleId = request.getParameter("roleListById") == null ? 0 : Integer.parseInt(request.getParameter("roleListById"));
Map<String, Object> properties = new HashMap<String, Object>();
if(roleId.intValue() > 0) {
properties.put("role:=", roleId);
model.put("roleId", roleId);
}
List<SystemAdministrator> administrator = systemAdminBiz.find(properties);
List<SystemRole> systemRole = systemRoleBiz.getAll();
model.put("administratorList", administrator);
model.put("roleList", systemRole);
return "sys_admin_list";
}
```
三、Spring MVC 默认采用的是转发来定位视图,如果要使用重定向,可以如下操作
1.使用RedirectView
```
public ModelAndView login(){
RedirectView view = new RedirectView("regirst.do");
return new ModelAndView(view);
}
```
2.使用redirect:前缀,工作中常用的方法:
```
public String login(){
//TODO
return "redirect:regirst.do";
}
```