Spring foreground interface and control interaction modelAndView

接收参数的方式:
1.HttpServletRequest方式接收
?
1
2
3
4
5
6
7
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方式
?
1
2
3
4
5
public ModelAndView test2(String userName,
     @RequestParam("password") String pwd){
   System.out.println(userName+","+pwd);
   return new ModelAndView("jsp/hello");
}
3. How to receive objects
?
1
2
3
4
public ModelAndView test3(User user){
   System.out.println(user);
   return new ModelAndView("jsp/hello") ;
}
4.
?
1
2
3
4
5
6
7
8
9
10
/**
  * Use ModelAndView outgoing parameters to pass the Attribute of the internal HttpServletRequest to the jsp page
   * ModelAndView(String viewName, Map data) data is the processing result
  */
@RequestMapping( "action")
public ModelAndView test4(User user){
   Map<String, Object> data = new HashMap<String, Object>();
   data.put("user", user);
   return new ModelAndView("jsp/hello",data);
}
5. Session的方式
?
1
2
3
4
5
6
7
8
9
/**
   * session存储  可以使用HttpServletRequest的getSession方法访问
   */
  @RequestMapping("action")
  public ModelAndView test7(HttpServletRequest req){
    HttpSession session = req.getSession();
    session.setAttribute("salary", 6000.0);
    return new ModelAndView("jsp/hello");
  }
6.重定向:
?
1
2
3
4
5
6
7
8
9
@RequestMapping("/updateitem")
//spirngMvc can directly receive the pojo type: the name attribute name of the input box on the page must be equal to the pojo attribute name
public ModelAndView updateitem(Items items){
 
itemsService.updateitems (items);
 
//Cannot add slashes to parse itemList.action
return new ModelAndView(new RedirectView("itemList.action"));
}
7. Redirect
?
1
2
3
4
5
6
7
8
@RequestMapping("/ updateitem")
//spirngMvc can directly receive the pojo type: it is required that the name attribute name of the input box on the page must be equal to the attribute name of the pojo
public String updateitem(Items items){
 
itemsService.updateitems(items);
//Redirecting to action can be resolved by adding a slash redirect:/itemList.action
return "redirect:itemList.action";
}
The effect of using Model is the same as ModelMap. If you use Model directly, springmvc will instantiate ModelMap.
If you use Model, you can not use the ModelAndView object, the Model object can pass data to the page, and the View object can be replaced by a String return value. Whether it is Model or ModelAndView, its essence is to use the Request object to pass data to jsp.
The above summary of SpringMvc's method of receiving parameters (must-read) is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Builder more.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326212980&siteId=291194637