MVC method Return Type
ModelAndView Return Value Type:
1. When the return is null, the page does not jump.
2. When the return value is not specified view name, view name as the default name to request a jump.
3. When the return value specifies the name of the view, the program will jump by view name.
/*添加*/ @RequestMapping("/getSale") public ModelAndView addSale(Sale sale,HttpServletRequest request,ModelAndView mv){ if (sale!=null) { Double totalPrice = sale.getPrice() * sale.getQuantity(); sale.setTotalPrice(totalPrice); sale.setSaleDate(new Date()); Users user = (Users) request.getSession().getAttribute("user"); sale.setUserId(user.getUid()); int i = userService.addSale(sale); if (i > 0) { mv.setViewName("saleList"); } else { mv.setViewName("prodectAdd"); } } return mv; }
Objectl return type;
/*绑定下拉框*/ @RequestMapping("/prodectName") @ResponseBody public Object getprodectName(){ List<Product> products = userService.getproductName(); return products; }
String Return Value Type:
1, if the return value is null, then a jump to request the name of the view name
2, if the return value is specified, then a jump according to the specified view name as the return value, may carry data model, modeMap.
3, if the return value with a forward or redirect prefix, or the corresponding request will be redirected, but can not carry data mvc data model, the data may be carried by ServletApi.
@RequestMapping ( " / available for purchase " ) public String available for purchase (String the userName, the Model Model) { // save the user name corresponding to the scope model.addAttribute ( " the userName " , the userName); return " available for purchase " ; }
MVC parameter passing
Request parameter automatic conversion
JSP page
form class="loginForm" action="/user/getUser" method="post" > <div class="inputbox" style="text-align:center; "> <label for="user">用户名:</label> <input id="user" type="text" name="userName"Please enter your user name"= placeholder" /> </div> <div class="password" style="text-align:center; " > <label for="mima">密码:</label> <input id="mima" type="password" name="password" placeholder="请输入密码" /> </div> <div class="subBtn" style="text-align:center; "> <input type="submit" value="登录" /> <input type="reset" value="重置"/> </div> </form>
Point * Note: Method Parameter Name in the Controller must be consistent with the value of the name attribute of the form element
/*登录*/ @RequestMapping("/getUser") @ResponseBody private ModelAndView getUser(String userName, String password, ModelAndView mv, HttpServletRequest request, HttpServletResponse response, HttpSession session){ Users user = userService.getUser(userName,password); System.out.println("user======"+user); if (user!=null){ System.out.println("成功"); //登录成功 request.getSession().setAttribute("user",user); //转发 mv.setViewName("index"); }else{ //登录失败 mv.setViewName("login"); } return mv; }
Assembly of the object request parameters POJO
New Person
public class Person { private String username; private int age; //省略get/set方法 }
Controller
// When the entity name attribute class attribute names and forms of the same element, to complete the automatic assembly @RequestMapping (value = " personObject " , Method = RequestMethod.POST) public String personObject (the Person Person) { the System. OUT .println (Person); return " Hello " ; }