码迷,mamicode.com
首页 > 编程语言 > 详细

Spring MVC的参数装配

时间:2019-11-11 09:38:08      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:pos   post   param   and   restful   pre   nbsp   class   his   

自动装配

  零散数据的装配

  装配原则:传递参数名和方法接收参数名一致

  表单

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="/threeController/myform" method="post">
        账号:<input type="text" name="uname"><br>
        密码:<input type="password" name="upwd">
        <input type="submit" value="提交">
    </form>
</body>
</html>

  目标页面

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>欢迎</title>
</head>
<body>
欢迎, ${userCode}
</body>
</html>

  控制器

@Controller
@RequestMapping("/threeController")
public class threeController {
    @RequestMapping("/myform")
    public ModelAndView myform(String uname, String upwd){
        ModelAndView mv=new ModelAndView();
        System.out.println(uname);
        System.out.println(upwd);
        mv.addObject("userCode",uname);
        mv.setViewName("myform");
        return mv;
    }
}

技术图片

 

 

 技术图片

 

 

 技术图片

  手动装配

 

  手动装配@RequestParam name代表页面发送的参数名字

  required代表参数是否必须传递 false代表可以不传递,默认为true defaultValue代表默认

  model代表给页面传递的数据

技术图片
@RequestMapping("/myform")
    public ModelAndView myform(@RequestParam(name="uname",required=false,defaultValue="梅川酷子")String uname, 
                   @RequestParam(name="upwd",required=false,defaultValue="admin")String upwd){ ModelAndView mv=new ModelAndView(); System.out.println(uname); System.out.println(upwd); mv.addObject("userCode",uname); mv.setViewName("myform"); return mv; }

 

  restful风格

  get请求时,如果需要传递数据,那么则不能使用以往方式?name=xxx&age=yy

  但是现在要遵循restful风格,举例 xxx/yyy/zzz
  根据地址栏URL匹配拿值 使用@PathVariable(name=地址栏中的参数映射)

  说白了就是:除了 / 以外的符号不能出现

@RequestMapping(value = "/myform2/{uname}/{upwd}")
    public ModelAndView myform2(@PathVariable(name="uname")String uname, @PathVariable(name="upwd")String upwd){
        ModelAndView mv=new ModelAndView();
        System.out.println(uname);
        System.out.println(upwd);
        mv.addObject("userCode",uname);
        mv.setViewName("myform");
        return mv;
    }

 

 

 

技术图片

 

  对象参数:

  传递的对象参数和对象中的属性名保持一致

public class UserInfo implements Serializable {
    private Integer user_id;
    private  String user_name;

    /**
     * 注入域属性
     * @return
     */
    private List<Teacher> teaList;
    private Teacher teacher;

    /*public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }*/

    public Integer getUser_id() {
        return user_id;
    }

    public void setUser_id(Integer user_id) {
        this.user_id = user_id;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public List<Teacher> getTeaList() {
        return teaList;
    }

    public void setTeaList(List<Teacher> teaList) {
        this.teaList = teaList;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "user_id=" + user_id +
                ", user_name=‘" + user_name + ‘\‘‘ +
                ", teaList=" + teaList +
                ", teacher=" + teacher +
                ‘}‘;
    }
}

  页面代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="/threeController/myform3" method="post">
        账号:<input type="text" name="uname"><br>
        密码:<input type="password" name="upwd"><br>
        info.name:<input type="text" name="user_name"/><br>
        info.id:<input type="text" name="user_id"/><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

  目标页面

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>欢迎</title>
</head>
<body>
欢迎, ${userCode}
</body>
</html>

  控制器

@RequestMapping(value = "/myform3")
    public ModelAndView myform3(UserInfo info){
        ModelAndView mv=new ModelAndView();
        System.out.println(info.toString());
        mv.addObject("userCode",info.getUser_name());
        mv.setViewName("myform");
        return mv;
    }

技术图片

技术图片

 

 

  域属性

  域属性传递:传递参数为:域属性.属性名

  页面代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="/threeController/myform4" method="post">
        账号:<input type="text" name="uname"><br>
        密码:<input type="password" name="upwd"><br>
        info.name:<input type="text" name="Teacher.teacher_name"/><br>
        info.id:<input type="text" name="Teacher.teacher_id"/><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

技术图片

 

技术图片

 

  集合参数传递

  集合参数传递:集合名[下标].属性名

  页面代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="/threeController/myform5" method="post">
        账号:<input type="text" name="uname"><br>
        密码:<input type="password" name="upwd"><br>
        info.name:<input type="text" name="teaList[0].teacher_name"/><br>
        info.id:<input type="text" name="teaList[1].teacher_id"/><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>

 

 

 

Spring MVC的参数装配

标签:pos   post   param   and   restful   pre   nbsp   class   his   

原文地址:https://www.cnblogs.com/F017/p/11832754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!