SpringMVC parameter binding of Day24SSM***

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
    username=jack
    passwrod=123456
    ""The traditional way
    is directly declared on the formal parameters of the method in the Controller HttpServletRequest, HttpServletResponse, HttpSession
    "" directly use the obtained value as a formal parameter. It
    must be ensured that the name of the parameter passed in is consistent with the name of the formal parameter of this method.
    url?cid=1 The corresponding method parameter is xx (String cid)
    springmvc There is a default type converter inside: String ---->int
    can only convert basic types and strings

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 username1,String password){
    
    
        //springmvc会将请求对象 赋值给req,参数绑定就是自动从req,将参数值
        //取出来赋值给username password
        System.out.println(username1);
        System.out.println(password);
        ModelAndView mv= new ModelAndView();
        mv.setViewName("main");
        return mv;
    }

SpringMVC parameter binding-basic type binding

》》@RequestParam Annotation
If the name of your formal parameter is inconsistent with the name of the parameter passed in the page, you can use the @RequestParam
page to pass: username=jack&password=1234
background parameter: test03(Strnig username1,String password)
as long as the value in the annotation is consistent with the parameter name passed in the page, the
background parameter: public ModelAndView test03( @RequestParam("username") String username1, String password)
required = true: Indicates 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, it will be obtained. If the parameter is not given, the default will be For 123

  @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?
    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 data of the page to the JavaBean
    JavaBean类中成员变量的名字和必须和表单中name属性的值一样
    Insert picture description here
@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 different types of member variables
    当前的javaBean类的成员变量有复杂类型 如:Person类中有birthday变量
public class A{
    
    
    private String name;
    private  B  b;
}

test05

  @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;
    }

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>

Person

public class 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;

Guess you like

Origin blog.csdn.net/u013621398/article/details/109050619