一、SpringMVC接收页面参数
1、 使用HttpServletRequest获取 :实例 http://*****//getHead?city="110100"&recType=0
@RequestMapping(value = "/getHead")
@ResponseBodypublic String getHeadData(HttpServletRequest request) {String city = request.getParameter("city"); String recType = request.getParameter("recType");
2、Spring会自动将form表单参数注入到方法参数,和表单的name属性保持一致。和Struts2一样
@RequestMapping("/login.do")
public String login(HttpServletRequest request,String name, @RequestParam("pass")String password)
// 表单属性是pass,用变量password接收
{
System.out.println(password);
}
3、自动注入Bean属性,实体类的属性和form表单中属性相同
<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)
{
System.out.println(user.getName());
System.out.println(user.getPass());
}
如果表单简单或者业务逻辑简单,我们或许一直也不会遇到什么麻烦,以上代码能很好的工作。 但是,如果哪天我们有了新的业务需要求,需要在这个表单中同时加上一些其它的内容,例如,要把业务员的资料也一起录入进去。 其中业务员的实体类定义如下:
<span style="font-size:12px;">public class Salesman
{
public string Name { get; set; }
public string Tel { get; set; }
}</span>
Controller的接口需要修改成:
public ActionResult Submit(Customer customer, Salesman salesman)
{
}
这时,HTML表单又该怎么写呢?刚好,这二个类的(部分)属性名称一样,显然,前面表单中的Name,Tel就无法对应了。 此时我们可以将表单写成如下形式:
<span style="font-size:12px;"><form action="/Home/Submit" method="post">
<p>客户名称: <input type="text" name="customer.Name" style="width: 300px" /></p>
<p>客户电话: <input type="text" name="customer.Tel" style="width: 300px" /></p>
<p>销售员名称: <input type="text" name="salesman.Name" style="width: 300px" /></p>
<p>销售员电话: <input type="text" name="salesman.Tel" style="width: 300px" /></p>
<p><input type="submit" value="提交" /></p>
</form></span>
注意Controller方法中的参数名与HTML表单中的name是有关系的...4、通过用注解@RequestParam绑定请求参数a到变量a。自动映射URL对应的参数到Action上面的数值,RequestParam 默认为必填参数。当请求参数a不存在时会有异常发生,可以通过设置属性required=false解决,如: @RequestParam(value="a", required=false),需要请求:http://***/get?a="变量值"
@RequestMapping(value = "/requestParam", method = RequestMethod.GET)
public String setupForm(@RequestParam("a") String a, ModelMap model) {
System.out.println(a);
return "helloWorld";}
5、通过@PathVariabl注解获取路径中传递参数,将参数作为请求路径的一部分
@RequestMapping(value = "/{id}/{str}")
public ModelAndView helloWorld(@PathVariable String id,@PathVariable String str) {
System.out.println(id);System.out.println(str);
return new ModelAndView("/helloWorld");
}
二、
向页面传值:当Controller组件处理后,向jsp页面传值,
1,使用HttpServletRequest 和 Session 然后setAttribute(),就和Servlet中一样
2,使用ModelAndView对象,Model,Map,RedirectAttribute
3,使用ModelMap对象
4,使用@ModelAttribute注解
1、ModelAndView数据会利用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、使用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";
}
3、使用@ModelAttribute示例。在Controller方法的参数部分或Bean属性方法上使用,@ModelAttribute数据会利用HttpServletRequest的Attribute传值到success.jsp中
@RequestMapping("/login.do")
public String login(@ModelAttribute("user") User user){
return "success";
}
@ModelAttribute("name")
public String getName(){
return name;
}
4、Session存储:可以利用HttpServletReequest的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";
}
三、Spring MVC 默认采用的是转发来定位视图,如果要使用重定向,可以如下操作
1,使用RedirectView
2,使用redirect:前缀
public ModelAndView login(){
RedirectView view = new RedirectView("regirst.do");
return new ModelAndView(view);
}
或者常用的:
public String login(){
//TODO
return "redirect:regirst.do";
}
后期持续整理......