Spring MVC: A Common Way for Controller to Receive Request Parameters

1. Receiving request parameters through entity bean s

For get and post submission requests, it is important to note that the bean's attribute name must be the same as the request parameter name.

Example: We submit a form on register.jsp page. There are three submissions: uname, upass, urepass, that is, user name, user password, user reconfirmed password. Write name="uname", "upass", "reupass" in the name attribute of the three <input/> tags in the form, and then submit the three names and jump to the specified path page of the action attribute of the form when submitting the form.

The submission request of the form will be captured by the controller Controller of the submission destination page path. How to get the form information after the capture?

At this point, you need to write a UserForm.java, that is, the entity bean class encapsulating form information, which takes uname, upass and urepass as attributes of the class (the attribute of the class must be the same as the name value of the submission form, and the setter() getter() method in the class can be added with the constructor). Spring MVC will automatically assign the submission information of the form. Attributes of entity beans can be passed in and used in Controller's method.

/*UserForm Object user receives request parameters submitted by registration page*/
public String register(UserForm user, Model model){
    if("zhangsan".equals(user.getUname()) && "123456".equals(user.getUpass()))
        return "login";//Register successfully, jump to login.jsp
    else{
        model.addAttribute("uname",user.getUname());//Load the input user name into the model and use the EL expression to extract the value from the model on the registration page.
        return "register";//Registration failed, return to the registration page
    }
}

2. Receiving the parameters of the request by processing the parameters

It is suitable for get and post submission requests. It receives the request parameters by processing the parameters of the method. That is to say, it directly writes the form parameters in the parameters of the corresponding method of Controller, that is, the name of the parameters and the name of the request parameters are exactly the same.

/*Receiving the request parameter by parameter, the parameter name is exactly the same as the request parameter name.*/
public String register(String uname,String upass, Model model){
    if("zhangsan".equals(uname) && "123456".equals(upass))
        return "login";//Register successfully, jump to login.jsp
    else{
        model.addAttribute("uname",uname);//Load the input user name into the model and use the EL expression to extract the value from the model on the registration page.
        return "register";//Registration failed, return to the registration page
    }
}

3. Receiving request parameters through HttpServletRequest

For get and post submission requests.

/*Receiving request parameters through HttpServletRequest*/
public String register(HttpServletRequest request, Model model){
    String uname=request.getParameter("uname");
    String upass=request.getParameter("upass");

    if("zhangsan".equals(uname) && "123456".equals(upass))
        return "login";//Register successfully, jump to login.jsp
    else{
        model.addAttribute("uname",uname);//Load the input user name into the model and use the EL expression to extract the value from the model on the registration page.
        return "register";//Registration failed, return to the registration page
    }
}

IV. Receiving the request parameters in the URL through @PathVariable

@RequestMapping(value="/register/{uname}/{upass}",method=RequestMethod.GET)
//method attribute must be added
/*Get the parameters in the URL through @PathVariable*/
public String register(@PathVariable String uname,@PathVariable String upass,Model model){
    if("zhangsan".equals(uname) && "123456".equals(upass))
        return "login";//Register successfully, jump to login.jsp
    else{
        model.addAttribute("uname",uname);//Load the input user name into the model and use the EL expression to extract the value from the model on the registration page.
        return "register";//Registration failed, return to the registration page
    }
}

V. Receiving parameters through @RequestParam

Applicable to get and post request parameters. The difference between receiving request parameters through @RequestParam and receiving request parameters through processing method is that when the request parameter name is inconsistent with the receiving parameter name, receiving request parameters through processing method will not report 404 errors, while @RequestParam receiving request parameters will report 404 errors.

/*Get the parameters in the URL through @RequestParam*/
public String register(@RequestParam String uname,@RequestParam String upass,Model model){
    if("zhangsan".equals(uname) && "123456".equals(upass))
        return "login";//Register successfully, jump to login.jsp
    else{
        model.addAttribute("uname",uname);//Load the input user name into the model and use the EL expression to extract the value from the model on the registration page.
        return "register";//Registration failed, return to the registration page
    }
}

6. Receiving request parameters through @ModelAttribute

For get and post submission requests. @ ModelAttribute annotation is used to encapsulate multiple request parameters into an entity object when freeing the formal parameters of the processing method, which simplifies the data binding process and automatically exposes the model data for display on the view page. The first method just encapsulates multiple request parameters into an entity object, and does not expose the model data (model.addAttribute() statement is required to expose the model data).

public String register(@ModelAttribute("user") UserForm user, Model model){
    if("zhangsan".equals(user.getUname()) && "123456".equals(user.getUpass()))
        return "login";//Register successfully, jump to login.jsp
    else{
        //Using @ModelAttribute("user") has the same function as model. addAttribute ("user").
        //Load the input user name into the model and use the EL expression to extract the value from the model on the registration page.
        return "register";//Registration failed, return to the registration page
    }
}

 

Keywords: JSP Attribute Java Spring

Added by radi8 on Tue, 30 Jul 2019 09:49:39 +0300