Home > Java > javaTutorial > body text

SpringMvc receives parameters

巴扎黑
Release: 2017-06-26 11:32:20
Original
1324 people have browsed it
接收参数的方式:
1.HttpServletRequest方式接收
 public ModelAndView test1(HttpServletRequest req){
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        System.out.println(userName);
        System.out.println(password);
        return new ModelAndView("jsp/hello");
    }
2.@RequestParam方式
  public ModelAndView test2(String userName,
            @RequestParam("password") String pwd){
        System.out.println(userName+","+pwd);
        return new ModelAndView("jsp/hello");
    }
3.对象的方式接收
 public ModelAndView test3(User user){
        System.out.println(user);
        return new ModelAndView("jsp/hello");
    }
4.
   /**
* Use ModelAndView to pass out parameters. The Attribute of the internal HttpServletRequest is passed to the jsp page
* ModelAndView(String viewName,Map data)data is the processing result
*/
@RequestMapping("action")
public ModelAndView test4(User user){
     Map data = new HashMap();
     data.put("user", user);
     return new ModelAndView("jsp/hello",data);
}
 
5. Session的方式
/**
* session storage can be accessed using the getSession method of HttpServletRequest
*/
    @RequestMapping("action")
    public ModelAndView test7(HttpServletRequest req){
        HttpSession session = req.getSession();
        session.setAttribute("salary", 6000.0);
        return new ModelAndView("jsp/hello");
    }
 
6.重定向:
@RequestMapping("/updateitem")
//spirngMvc可以直接接收pojo类型:要求页面上input框的name属性名称必须等于pojo的属性名称
public ModelAndView updateitem(Items items){
 
itemsService.updateitems(items);
 
//不可以加斜杠 解析不了 itemList.action
return new ModelAndView(new RedirectView("itemList.action"));
}
 
7.重定向
@RequestMapping("/updateitem")
//spirngMvc可以直接接收pojo类型:要求页面上input框的name属性名称必须等于pojo的属性名称
public String updateitem(Items items){
 
itemsService.updateitems(items);
//重定向到action 可以加斜杠 redirect:/itemList.action 解析的了
return "redirect:itemList.action";
}
 
 
 

使用Model和ModelMap的效果一样,如果直接使用Model,springmvc会实例化ModelMap。

如果使用Model则可以不使用ModelAndView对象,Model对象可以向页面传递数据,View对象则可以使用String返回值替代。不管是Model还是ModelAndView,其本质都是使用Request对象向jsp传递数据。

The above is the detailed content of SpringMvc receives parameters. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!