Day24 SpringMVC parameter binding***

SpringMVC parameter binding-basic type binding

1. 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 way
    Declare HttpServletRequest, HttpServletResponse, HttpSession directly on the formal parameters of the method in the Controller
  • Directly use the obtained value as a formal parameter. It
    must be guaranteed: 传入的参数的名字和本方法形参的名字一致
    that is url?cid=1 对应方法的参数是xx (String cid)
    , the internal springmvc has 默认的类型转换器: String ---->int只能转换基本类型和字符串
  • Example

Request url
Insert picture description here
Insert picture description here

ParameterController

@Controller
public class ParameterController {
    
    
    //传统方式
    @RequestMapping("login1.action")
    public ModelAndView test01(HttpServletRequest req){
    
    
        //springmvc会将请求对象 赋值给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 username,String password){
    
    
        //springmvc会将请求对象 赋值给req
        //参数绑定就是自动从req中 将参数值取出来 赋值给username和password
        System.out.println(username);
        System.out.println(password);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("main");//请求转发 跳转到主页
        return mv;
    }

2. @RequestParam annotation

  • (1) If the name of your formal parameter is inconsistent with the name of the parameter passed in the page,
    such as:
    request url, the page is passed: username=jack&password=1234
    background method parameters:test03(Strnig username1,String password)
  • (2) You can use @RequestParam to solve.
    As long as the value in the annotation is consistent with the parameter name passed in on the page, you can
    change the background method parameter to: public ModelAndView test03( @RequestParam("username")String username1, String password)
  • (3) Three attributes
    value = "username": If the value is consistent with the parameter name passed in the page, it can be assigned to username1
    required = true: it means that the parameter must be given a value. If this value is not given, the 400 page will appear
    defaultValue = “123”: if the parameter is given, get it. If no parameter is given, the default is 123
  • Example
    Insert picture description here

ParameterController

  @RequestMapping("login3.action")
//    public ModelAndView test03(@RequestParam("username") String username1, String password){
    
    
    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;
    }

SpringMVC parameter binding-pojo binding

1. What is pojo?

POJO (Plain Ordinary Java Object) simple Java objects are actually ordinary JavaBeans
其中有一些属性及其getter setter方法的类,没有业务逻辑

2. How to use it?

When the page needs to pass multiple parameters (register), we can encapsulate multiple parameters into a JavaBean class, and use this JavaBean as a method parameter. SpringMVC can directly assign the page data to the JavaBean
JavaBean类中成员变量的名字和必须和表单中name属性的值一样

  • Example

Define a pojo class

public class MyPojo {
    
    
    private String username;
    private String password;
    //省略getset
}    

Request url
Insert picture description here
ParameterController

	@RequestMapping("login4.action")
    public ModelAndView test04(MyPojo mypojo){
    
    
        //springMVC可以将表单的数据赋值给 一个javaBean对象
        System.out.println(mypojo.getUsername());
        System.out.println(mypojo.getPassword());
        ModelAndView mv= new ModelAndView();
        mv.setViewName("main");
        return mv;
    }

SpringMVC parameter binding-queryvo binding

1. What is queryVo?

As a wrapper class, queryVo can encapsulate multiple member variables of different types
当前的javaBean类的成员变量有复杂类型,如:Person类中有birthday变量

  • Example

Person

public class Person {
    
    
    private int id;
    private String username;
    private String password;
    private String city;
    private Birthday birthday;
	//省略getset
}

Birthday

public class Birthday {
    
    
    private int year;
    private int month;
    private int day;
	//省略getset
}

addPersonUI.jsp

<form method="post" action="${pageContext.request.contextPath}/add.action">
           用户名: <input type="text" name="username" /><br/>
           用户密码: <input type="text" name="password"/><br/>
           城市: <input type="text" name="city"/><br/><input type="text" name="birthday.year"/><br/><input type="text" name="birthday.month"/><br/><input type="text" name="birthday.day"/><br/>
            <input type="submit" value="添加"/><br/>
</form>

ParameterController

  @RequestMapping("add.action")
    public ModelAndView test05(Person person){
    
    
        //springMVC可以将表单的数据赋值给 一个javaBean对象
        System.out.println(person.getUsername());
        System.out.println(person.getPassword());
        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;
    }

Guess you like

Origin blog.csdn.net/qq_43639081/article/details/109150050