SSM之SpringMVC参数绑定 基本类型绑定 pojo绑定 queryvo绑定

基本类型绑定

  • 什么是参数绑定
    用户请求服务器的时候会给后台传递参数,如何来快速的接收到用户传递的参数
    》传统方式:在Controller中的方法的形式参数上直接声明HttpServletRequest,HttpServletResponse,HttpSession
    》直接将获取的值作为形参 必须保证传入的参数的名字和本方法形参的名称一致 url?cid=1对应方法的参数是xx(String cid)
    springmvc的内部有默认的类型转换器:String---->int 只能转换基本类型和字符串

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

@RequestParam注解

如果形参的名字和页面传入的参数名字不一致,可以使用@RequestParam
只要注解中value值和页面传入的参数名字一致即可
后台参数:public ModelAndView test03(@RequestParam(“username”) String username1, String password)
required = true 表示该参数必须给值,如果没有这个值,则会出现400页面
defaultValue=”123“表示如果给了参数就获取,如果没有给参数,则默认为123

 @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绑定

  • pojo是什么?
    POJO(Plain Ordinary Java Object)简单的Java对象,实际上就是普通的JavaBeans 其中有一些属性及其getter setter方法的类,没有业务逻辑
  • 如何使用
    当页面需要传递多个参数时(注册),我们可以将多个参数封装到一个JavaBean类,将这个JavaBean作为方法形参,SpringMVC可以直接将页面的数据赋值给JavaBean JavaBean类中成员变量的名字必须和表单中的name属性的值一样
 @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;
    }

queryvo绑定

  • queryVo是什么
    queryVo作为一个包装类,可以封装多个不同类型的成员变量 当前的javaBean类的成员变量有复杂类型

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">
    用户名: <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

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;

猜你喜欢

转载自blog.csdn.net/xinxin_____/article/details/109059901