Spring MVC parameter binding of SSM basic type binding pojo binding queryvo binding

Basic type binding

  • What is parameter binding
    When the user requests the server, it will pass parameters to the background. How to quickly receive the parameters passed by the user
    >Traditional method: directly declare HttpServletRequest, HttpServletResponse and HttpSession on the formal parameters of the method in the Controller
    >Directly take the obtained value as a formal parameter. You must ensure that the name of the passed in parameter is consistent with the name of the formal parameter of this method? cid=1. The parameter of the corresponding method is xx(String cid)
    Spring MVC has a default type converter inside: String - > int can only convert basic types and strings

ParameterController

@RequestMapping("login1.action")
    public ModelAndView test01(HttpServletRequest req){
        //Spring MVC assigns the request object to req
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("main");
        return mv;
    }
    @RequestMapping("login2.action")
    public ModelAndView test02(String username1,String password){
        //Spring MVC will assign the request object to req, and the parameter binding will automatically take out the parameter value from req and assign it to username password
        System.out.println(username1);
        System.out.println(password);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("main");
        return mv;
    }

@RequestParam annotation

If the name of the formal parameter is inconsistent with the name of the parameter passed in from the page, you can use @ RequestParam
As long as the value in the annotation is consistent with the parameter name passed in from the page
Background parameters: public ModelAndView test03(@RequestParam("username") String username1, String password)
required = true means that the parameter must be given a value. If there is no value, the 400 page will appear
defaultValue = "123" means that if the parameter is given, it will be obtained. If the parameter is not given, it will be 123 by default

 @RequestMapping("login3.action")
    public ModelAndView test03(@RequestParam(value="name",required = true,defaultValue = "rose")String username1,String password){
        System.out.println(username1);
        System.out.println(password);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("main");
        return mv;
    }

pojo binding

  • What is pojo?
    POJO (Plain Ordinary Java Object) is a simple java object, which is actually ordinary JavaBeans. There are some properties and classes of their getter setter methods, and there is no business logic
  • How to use
    When the page needs to pass multiple parameters (Registration), we can encapsulate multiple parameters into a JavaBean class and take the JavaBean as a method parameter. Spring MVC can directly assign the data of the page to the JavaBean. The name of the member variable in the JavaBean class must be the same as the value of the name attribute in the form
 @RequestMapping("login4.action")
    public ModelAndView test04(MyPojo myPojo){
        //Spring MVC can assign the data of a form to a JavaBean object
        System.out.println(myPojo.getUsername());
        System.out.println(myPojo.getPassword());
        ModelAndView mv =new ModelAndView();
        mv.setViewName("main");
        return mv;
    }

queryvo binding

  • What is queryVo
    As a wrapper class, queryVo can encapsulate multiple member variables of different types. The member variables of the current javaBean class have complex types

test05

 @RequestMapping("add.action")
    public ModelAndView test05(Person person){
        System.out.println(person.getUsername());
        System.out.println(person.getPassword());
        System.out.println(person.getCity());
        System.out.println(person.getBirthday().getYear());
        System.out.println(person.getBirthday().getMonth());
        System.out.println(person.getBirthday().getDay());
        ModelAndView mv = new ModelAndView();
        mv.setViewName("main");
        return mv;
    }

addPersonUI.jsp

<form method="post" action="${pageContext.request.contextPath}/add.action">
    user name: <input type="text" name="username" /><br/>
    User password: <input type="text" name="password"/><br/>
    city: <input type="text" name="city"/><br/>
    year <input type="text" name="birthday.year"/><br/>
    month<input type="text" name="birthday.month"/><br/>
    day<input type="text" name="birthday.day"/><br/>
    <input type="submit" value="add to"/><br/>
</form>

Person

private int id;
    private String username;
    private String password;
    private String city;
    private Birthday birthday;

Birthday

public class Birthday {
    private int year;
    private int month;
    private int day;

Tags: Java Spring mvc

Posted by jwwceo on Thu, 12 May 2022 04:30:31 +0300