美文网首页
参数绑定与传值

参数绑定与传值

作者: 神豪VS勇士赢 | 来源:发表于2018-08-07 19:38 被阅读1次

    参数绑定与传值
    1)功能方法之间的跳转
    2)功能方法之间的传值
    3)方法和页面之间传值(重点)
    4)从页面到功能方法(V-C)

    功能方法之间的跳转

    方案一:ModelAndView
    携带跟路径
    

    可以在字符串里面加入redirect或者forward
    mv.setViewName("redirect|forward:/friend/listFri.do");
    mv.setViewName("/friend/listFriend.do");

    方案二:String
    

    方法返回String 类型return "/friend/listFriend.do"
    上面的方法和下面的写法一致:
    request.getRequestDispatcher("/friend/listFriend.do")

    功能方法之间跳转需要注意点: 路径问题:

    绝对路径:以/开始
    加跟路径的跳转:
    第一次:http://localhost:8080/user/add.do
    第二次:http://localhost:8080/user/find.do
    不加跟路径:
    第一次:http://localhost:8080/user/add.do
    第二次:http://localhost:8080/find.do 所以报错

    (如果使用了/开始,标示就是绝对路径,会在斜杠的前面加上默认路径http://localhost:8080/再访问
    ,这个默认路径在工作中有可能是一个域名,或者一个域名+项目名。)

    相对路径:不加跟路径,不加斜杠
    第一次:http://localhost:8080/user/add.do
    第二次:相对的上一个路径就是http://localhost:8080/user/,这个路径有跟路径。
    结果:http://localhost:8080/user/find.do

    功能方法之间的传值
    Request
    Session
    Cache(分布式缓存)
    存:request.setAttribute("friend","测试");内部走过滤器
    取:request.getAttribute("friend").toString();
    注意: 从定向跳转取不到request范围内的值

    某个方法设置值:
    @RequestMapping("/addCar.do")
    public String addCar(HttpServletRequest request){
    System.out.println("添加成功");
    request.setAttribute("tag","电动汽车");
    //跳转到列表 return "/listCar.do";
    }
    其他方法接收参数:
    @RequestMapping("/listCar.do")
    public ModelAndView listCar(HttpServletRequest request) {
    Object tag = request.getAttribute("tag");
    ModelAndView mv = new ModelAndView();
    //有两个能力,第一:传值,第二:跳转页面
    mv.addObject("tag",tag);
    mv.setViewName("/car.jsp");
    return mv;
    }

    方法和页面之间传值(重点)
    (一)从具体的功能方法到页面(从C-V)

    方案一:ModelAndView
    setViewName设置跳转地址
    addObject方法传数据

    @RequestMapping("first.do")
    public ModelAndView goToFirstPage(HttpServletRequest httpServletRequest){
    ModelAndView modelAndView=new ModelAndView();
    List<User> userList =new ArrayList<User>();
    User user1=new User();
    User user2=new User();
    user1.setName("张颖豪");
    user1.setPass("123");
    user2.setPass("12344");
    user2.setName("魏雪");
    userList.add(user1);
    userList.add(user2);
    modelAndView.addObject("firstInfo",userList);
    modelAndView.setViewName("/first/second.do");
    return modelAndView;
    }

    方案二:Model
    Model只能传数据,不能返回具体的页面地址。
    返回地址,需要依靠return一个路径。(返回类型需要是String)

        @RequestMapping("/third.do")
        public String goThirdPage(Model model){
        model.addAttribute("third","这是第三个值");
        return "/third.jsp";
        }
    

    (二) 从页面到功能方法(V-C)

    方案一:request.getPamerter

    在形参中添加HttpServletRequest request参数,通过request接收参数

    third.jsp 如下所示:

    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
    <title>Title</title>
    </head>
    <body>
    <form action="/first/third.do" method="post">
    用户名:<input type="text" name="username">

    密码:<input type="password" name="password">

    <input type="submit" value="提交">
    </form>
    </body>
    </html>

    third.do 如下所示:
    @RequestMapping("/third.do")
    public String goThirdPage(Model model,HttpServletRequest request){
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    System.out.println(username+"\t"+password);
    model.addAttribute("third",username+"&"+password);
    return "/index.jsp";
    }

    运行结果如下:


    image.png
    image.png

    方案二:指定具体的类型接受具体的参数

    接受整形类型Integer(传递错误不进Action)
    接受字符串类型String
    接受数组类型Integer[]
    只是针对基本类型。

    third.jsp 如下:

    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
    <head>
    <title>Title</title>
    </head>
    <body>
    <form action="/first/four.do" method="post">
    用户名:<input type="text" name="username">

    密码:<input type="password" name="password">

    爱好: <input type="checkbox" name="like" value="ball">篮球
    <input type="checkbox" name="like" value="girl">女孩

    <input type="submit" value="提交">
    </form>

    </body>
    </html>

    four.do 如下:
    @RequestMapping("/four.do")
    public String goToFour(String username,String password,String[] like,Model model){
    System.out.println(username+"\t"+password+"\t"+ Arrays.toString(like));
    model.addAttribute("four",username+"&"+password+"&"+Arrays.toString(like));
    return "/index.jsp";
    }

    index.jsp 如下:

    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html>
    <body>
    <h2>Hello World!</h2>
    ${four}
    </body>
    </html>

    运行结果如下:


    image.png image.png

    注意点: 只是针对基本类型。 并且只有我们输入值与实际上的字段类型对应才可以自动转换 ,例如我们输入的字符串 ,但是在 four.do 使用了 数值类型接收值的时候 会直接报错 。

    例如: 我们修改接收字段


    image.png

    运行:


    image.png

    点击提交之后 :


    image.png

    注意: 页面上传递的所有文本数据都可以通过String接收。
    注意: 复杂对象类型不可以直接接受:如Date类型

    方案三:使用封装类型Bean【DTO】来接受多个参数
    在形参中让包装类型dto接收参数。DTO中属性名称必须和页面上控件名称一致。

    Dto如下所示:

    public class Dto {
    private String username;
    private String password;
    private String[] like;

    public Dto() {
    }
    
    @Override
    public String toString() {
        return "Dto{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", like=" + Arrays.toString(like) +
                '}';
    }
    
    public Dto(String username, String password, String[] like) {
        this.username = username;
        this.password = password;
        this.like = like;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String[] getLike() {
        return like;
    }
    
    public void setLike(String[] like) {
        this.like = like;
    }
    

    }

    jsp界面仅仅 Action部分改变请求地址,其他都没有改变。

    five.do 如下所示:

    @RequestMapping("five.do")
    public String goToFive(Dto dto,Model model){
    String username = dto.getUsername();
    String password = dto.getPassword();
    String[] like = dto.getLike();
    System.out.println(username+"&"+password+"&"+Arrays.toString(like));
    model.addAttribute("five",username+"&"+password+Arrays.toString(like));
    return "/index.jsp";
    }

    测试输出结果:


    image.png image.png

    可以发现,一样可以完成。

    相关文章

      网友评论

          本文标题:参数绑定与传值

          本文链接:https://www.haomeiwen.com/subject/cybavftx.html