[SSM-SpringMVC Chapter] 03-SpringMVC parameter binding-automatic parameter binding-javabean object parameter binding-nested bean parameter binding

Parameter binding of SpringMVC

  When the user requests the server, the parameters (Controller) are passed to the background. How to quickly receive the parameters passed by the user can be achieved through parameter binding.

1 The traditional way of parameter transfer

  Declare HttpServletRequest, HttpServletResponse, HttpSession directly on the formal parameters of the method in the Controller (the previous JAVAEE acquisition method)

	//注解,通过这个地址login1.action执行当前方法
	@RequestMapping("test01.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;
    }

2 Realize through the parameter binding of SpringMVC [***Use this]

   For common data types (8 basic data types, String data types), parameter binding will be performed directly [springmvc has a default type converter, such as Spring -> int]; but if it is a custom data type or other Data type, you need to create a bean object class to encapsulate the call.

2.1 Parameter binding of the basic data types of SpringMVC

2.1.1 The method form parameter name is consistent with the page input parameter name

You only need to ensure that the name of the request scope request (that is, the value passed from the browser) is consistent with the formal parameter name on the method, and the value can be automatically assigned.

 @RequestMapping("test02.action")
    public ModelAndView test02(String username1,String password){
    
    
        //springmvc会将请求对象,赋值给request
        //参数绑定就是自动从requeest,将参数值取出来赋值给形式参数username password
        System.out.println(username1);
        System.out.println(password);
        ModelAndView mv= new ModelAndView();
        mv.setViewName("main");通过配置视图解析器前后缀,找到指定界面
        return mv;
    }
2.1.2 The method formal parameter name is inconsistent with the page input parameter name

If the name of your formal parameter is inconsistent with the name of the parameter passed in the page, you can use @RequestParamannotations to set the parameter name differently@RequestParam(value="页面的参数名")

Three values ​​of @RequestParam

  • value: the parameter name of the request parameter
  • required: whether the parameter is required, the default is true,
  • defaultValue: The default value of the request parameter, which means that the corresponding parameter must be included in the request parameter. If it does not exist, an exception will be thrown.

For example, the parameter names passed in from the page are username and password, and the receiving parameters are name and password. You need to add a comment to the name, which should be written as follows


public ModelAndView test03(@RequestParam(value="username") String name, String password){
    
    
		//需要将形式参数name标明只的是页面参数username
        System.out.println(name);
        System.out.println(password);
        ModelAndView mv= new ModelAndView();
        mv.setViewName("main");
        return mv;
    }

2.1.3 Summary

  • Basic data types and String parameters are automatically bound
  • springmvc will automatically assign the request object to the request, and the automatic parameter binding is to automatically assign the winning data from the request to the parameter list
  • Prerequisites (the parameter list and the name in the request field are the same, no value is assigned if they are different)
  • Automatically get the value from the request and assign it to the parameter list (the name must be the same, and only simple parameters (8 basic data types + String))
  • If the parameter name passed in from the page is different from the method formal parameter, it needs @RequestParamto be set through annotations (multiple settings can be set, and the order and formal parameters must be consistent)
  • If the parameter names are different, you can use annotations to @RequestParamremedy them. If there are no annotations, get the value according to the parameter list. If there are annotations, press the annotations first (see the annotations first)


2.2 SpringMVC object data type pojoparameter binding

Pojo concept:
  POJO (Plain Ordinary Java Object) is a simple Java object, which is actually ordinary JavaBeans. (Need to create the corresponding javabean class)
  There are only some properties and their getter setter methods, and no other business logic methods.

scenes to be used

  When the page needs to pass multiple parameters (a form, etc.), we can encapsulate multiple parameters into a JavaBean class. (Using POJO class for data encapsulation is to create different javabean object encapsulation).
  Taking this JavaBean as a method parameter, SpringMVC can directly assign the page data to the JavaBean object (parameter list parameters are automatically assigned).
   [Prerequisite] The name of the member variable in the JavaBean class must be the same as the value of the name attribute in the form (because the value assignment is through the get/set method) .

Case

Front page jsp

		<input type="text" name="username" placeholder="请输入用户名"/>
		<input type="password" name="password" placeholder="请输入密码"/>

We can encapsulate it into a User pojpobject

public class User{
    
    
	private String username;
	private String password;
	//省略get/set方法(一定得有)
}

Parameter binding:

@RequestMapping("test04.action")
	//从页面获取参数然后赋值给javabean对象的同名属性(通过set方法)
    public ModelAndView test04(User user){
    
    
        //springMVC可以将表单的数据赋值给一个javaBean对象
        //通过get方法获取已经被自动赋值的javabean对象属性
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        ModelAndView mv= new ModelAndView();
        mv.setViewName("main");
        return mv;
    }

2.3 SpringMVC's nested javabean parameter binding

The concept
 encapsulates multiple different types of javabean objects as member variables (that is, one javabean encapsulates multiple javabean objects as attributes).
JavaBean class member variables are the current complex type (other javabean) as: Person class has birthday variables
  by the current member attribute name (attribute name javabean object as a member) plus .point to retrieve its attributes
javabean

public class Person {
    
    
    private int id;
    private String username;
    private String password;
    private Birthday birthday;
    //省略get/set方法
}

public class Birthday {
    
    
    private int year;
    private int month;
    private int day;
    //省略get/set方法
}

Backstage acquisition method

  @RequestMapping("test05.action")
  	//按照name相同自动参数绑定赋值
    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;
    }

Front desk jsp writing

	    <form method="post" action="${pageContext.request.contextPath}/registerPerson.action">
           用户名: <input type="text" name="username" /><br/>
           用户密码: <input type="text" name="password"/><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>

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/109050480