Spring MVC基础知识整理➣View与Controller数据交互
概述
Spring MVC是由View—Controller—Model组成,其中View和Controller的数据交互,成为了关注的核心点。MVC中,我们将View中的数据传递到Controller,可以采用POST或者Get,传递相应的参数。Controller通过绑定来,匹配前台传递的参数。后台Controller也可以将值传递到前台页面。
View值传递Controller
绑定传值的常用方式有如下
- @RequestParam,绑定单个请求数据,可以是URL中的数据,表单提交的数据或上传的文件;
- @PathVariable,绑定URL模板变量值;
- @CookieValue,绑定Cookie数据;
- @RequestHeader,绑定请求头数据;
- @ModelAttribute,绑定数据到Model;
- @SessionAttributes,绑定数据到Session;
- @RequestBody,用来处理Content-Type不是application/x-www-form-urlencoded编码的内容,例如application/json, application/xml等;
@RequestParam和@PathVariable使用举例Controller如下:
@RequestMapping(value="/City/{cname:\\w+}-{id:\\d+}",method=RequestMethod.GET) public ModelAndView ChinaCity(@PathVariable(value="id") String ID,@PathVariable(value="cname") String Name,
@RequestParam("code") String UserCode) { ModelAndView modv=new ModelAndView(); modv.addObject("cityid", ID); modv.addObject("cname", Name); modv.addObject("code", UserCode); modv.setViewName("HelloWorld/ChinaCity"); return modv; }
匹配解析URL: http://www.xxx.com/MVCDEMO/HellowWorld/City/Beijing/23?code=988234
@CookieValue和@RequestHeader以及@ModelAttribute使用举例如下
@RequestMapping("/UserCook") public ModelAndView ShowUserSessionModel(@CookieValue("JSESSIONID") String CookName,@RequestHeader("User-Agent") String Userage,
@ModelAttribute("userinfo") UserInfo user) { ModelAndView RMV=new ModelAndView(); RMV.setViewName("User/UserCook"); RMV.addObject("Cookmsg", CookName+"-"+Userage); return RMV; }
解析:@CookieValue解析浏览器cookie值,@RequestHeader解析浏览器请求Header头部值,而@ModelAttribute解析Form表单绑定的Model值,对应Jsp如下
<from:form action="" method="post" modelAttribute="userinfo"> <p> <from:label path="username">用户名:</from:label><from:input path="username"/><from:errors path="username"></from:errors> </p> <p> <from:label path="userage">用户年龄:</from:label><from:input path="userage"/><from:errors path="userage"></from:errors> </p> <P> <input type="submit" value="提交 "> </P> </from:form>
@SessionAttributes 把数据存储到session中,来保持多次请求间数据,这样就可以来实现比如分步骤提交表单等需求。需要将SessionAttributes绑定Controller,之后Controller里面的方法能够调取Session值
package justin.com.controllers; import justin.com.models.UserInfo; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.bind.support.SessionStatus; @Controller @SessionAttributes("SessionUser") @RequestMapping("/USession") public class SessUserController { @RequestMapping(value="/Home",method=RequestMethod.GET) public String SessHome(@ModelAttribute("SessionUser") UserInfo user,SessionStatus status) { status.setComplete(); //读取Session中赋值给UserInfo if(user!=null) user.setUserage(100); return "USession/index"; } }
编写USession/Index.jsp页面
<body> SESSION Index ! <p> UserAGE: ${SessionUser.userage} </p> <p> UserName: ${SessionUser.username } </p> </body>
请求URL:
http://www.xxx.com/MVCDEMO/USession/Home?userage=98&username=justin
@RequestBody 调用合适的MessageConvert来把非application/x-www-form-urlencoded请求中的内容转换为指定的对象。
它通常与@ResponseBody合用。@ResponseBody与.@RequestBody刚好相反,他把指定的对象转换为合适的内容(请求头为Accept:application/json 则返回json数据)并返回。由于Spring默认解析json用的是Jackson,所以我们这里要把jackson-core-asl-1.9.13.jar和jackson-mapper-asl-1.9.13.jar两个包添加到我们项目。
举例使用Controller如下
@RequestMapping("/detail") public @ResponseBody UserInfo GetUserDetail() { UserInfo usermodel=new UserInfo(); usermodel.setUsername("zhang"); usermodel.setUserage(25); System.out.println("Update:"+usermodel.getUsername()); return usermodel; }
访问detail页面,反馈json值,针对Spring-MVC3有效,其他版本待定。
Controller值传递View
1、使用Model值
@RequestMapping(value="/detail",method=RequestMethod.GET) public String ShowUserDetail(HttpServletRequest request,Model model) { model.addAttribute("userinfo", new UserInfo()); return "User/UserHome"; }
2、使用ViewModel传值
ModelAndView Rm=new ModelAndView(); Rm.setViewName("User/UserShow"); Rm.addObject("userinfo", user);
3、使用HttpServletRequest
public String exception(HttpServletRequest request,Exception ex) { request.setAttribute("exceptionMessage", ex.getMessage()); return "error"; }
View获取值:
${key}