SpringMVC receive request parameters and page parameters
- 2020-05-19 04:59:47
- OfStack
spring receive request parameters:
1. Get it using HttpServletRequest
@RequestMapping("/login.do")
public String login(HttpServletRequest request){
String name = request.getParameter("name")
String pass = request.getParameter("pass")
}
2,Spring will automatically inject form parameters into method parameters, and the name property of the form will remain 1. And Struts21
@RequestMapping("/login.do")
public String login(HttpServletRequest request,
String name,
@RequestParam("pass")String password) // The form properties are pass, With variable password receive
{
syso(name);
syso(password)
}
3. Automatically inject Bean properties
<form action="login.do">
User name: <input name="name"/>
Password: <input name="pass"/>
<input type="submit" value=" landing ">
</form>
// encapsulated User class
public class User{
private String name;
private String pass;
}
@RequestMapping("/login.do")
public String login(User user)
{
syso(user.getName());
syso(user.getPass());
}
Pass value to page:
When the Controller component is processed, pass the value to the jsp page,
1, use HttpServletRequest and Session and then setAttribute(), just like 1 in Servlet
2, use the ModelAndView object
3, use the ModelMap object
4. Use the @ModelAttribute annotation
The Model data is passed to success.jsp using HttpServletRequest's Attribute
@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);
}
Example using ModelMap parameter object:
ModelMap data is passed to success.jsp using Attribute of HttpServletRequest
@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";
}
Use the @ModelAttribute example
Used on the arguments section of the Controller method or on the Bean attribute method
The @ModelAttribute data is passed to success.jsp using HttpServletRequest's Attribute
@RequestMapping("/login.do")
public String login(@ModelAttribute("user") User user){
//TODO
return "success";
}
@ModelAttribute("name")
public String getName(){
return name;
}
Session storage:
You can take advantage of HttpServletReequest's getSession() method
@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";
}
By default, Spring MVC USES forwarding to locate views. If you want to use redirection, you can do the following
1. Use RedirectView
2, use redirect: prefix
public ModelAndView login(){
RedirectView view = new RedirectView("regirst.do");
return new ModelAndView(view);
}
Or use the following methods, which are commonly used at work:
public String login(){
//TODO
return "redirect:regirst.do";
}