zoukankan      html  css  js  c++  java
  • MVC参数传递

    请求参数自动类型转换

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登陆</title>
    </head>
    <body>
    <form action="/fourth/oneRequest" method="post">
        账户:<input type="text" name="userName"/>
        密码:<input type="password" name="userpwd"/>
        <input type="submit" value="登陆"/>
    </form>
    </body>
    </html>

    (控制器Controller中的方法参数名称必须和表单元素的name属性值保持一致)
    
    @Controller
    @RequestMapping("/fourth")
    public class FourthController {
    
        /**
         * 1、请求参数的自动类型转换
         * @param userName
         * @param userpwd
         * @param model
         * @return
         * 控制器Controller中的方法参数名称必须和表单元素的name属性值保持一致
         */
        @RequestMapping(value = "/oneRequest")
        public String oneRequest(String userName,String userpwd, Model model){
            System.out.println(userName+"	"+userpwd);
            model.addAttribute("userCode",userName);
            return "welcome";
        }
    
    }

    @RequestParam注解

    jsp页面

     Controller

    RequestMethod.POST

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登陆</title>
    </head>
    <body>
    <form action="/fourth/twoRequest" method="post">
        账户:<input type="text" name="userName"/>
        密码:<input type="password" name="userpwd"/>
        <input type="submit" value="登陆"/>
    </form>
    </body>
    </html>

    Controller

    (此处必须设置请求类型,否则会显示405错误)
    
    /**
     * 2、RequestMethod.POST 此处必须设置请求类型 否则将会显示405错误
     * @param userName
     * @param userpwd
     * @param model
     * @return
     *  控制器Controller中的方法参数名称必须和表单元素的name属性值保持一致
     */
    @RequestMapping(value = "/twoRequest",method = RequestMethod.POST)
    public String twoRequest(String userName,String userpwd, Model model){
        System.out.println(userName+"	"+userpwd);
        model.addAttribute("userCode",userName);
        return "welcome";
    }

    RESTFUL风格的参数传递

     对象参数

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登陆</title>
    </head>
    <body>
    <form action="/fourth/Info" method="post">
        账户:<input type="text" name="userName"/>
        密码:<input type="password" name="userpwd"/>
        <input type="submit" value="登陆"/>
    </form>
    </body>
    </html>
    /**
     * 5、对象参数
     */
    @RequestMapping(value = "/Info")
    public String UserRequest(UserInfo info){
        System.out.println(info.getUserName());
        return "welcome";
    }

    域属性对象参数

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登陆</title>
    </head>
    <body>
    <form action="/fourth/userInfoRequest" method="post">
        teacher01:<input type="text" name="teacher.teacherName"/>
        <input type="submit" value="登陆"/>
    </form>
    </body>
    </html>
    /**
     * 6、域属性对象参数
     */
    @RequestMapping(value = "/userInfoRequest")
    public String UserInfoRequest(UserInfo info){
        System.out.println(info.getTeacher().getTeacherName());
        return "welcome";
    }

    域属性集合参数

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登陆</title>
    </head>
    <body>
    <form action="/fourth/userInfoRequest" method="post">
        teacher02:<input type="text" name="teacherList[0].teacherName"/>
        teacher03:<input type="text" name="teacherList[1].teacherName"/>
        <input type="submit" value="登陆"/>
    </form>
    </body>
    </html>
    /**
     * 7、域属性集合参数
     */
    @RequestMapping(value = "/userListRequest")
    public String UserListRequest(UserInfo info){
        System.out.println(info.getTeacherList());
        return "welcome";
    }
  • 相关阅读:
    请朋友做事,须以名誉为限,为朋友做事,亦须以名誉为限
    这世上总有一些人记得你,关注着你,牵挂着你
    杏花春雨已不再,牧童遥指已不再,剑门细雨渭城轻尘也都已不再
    如果要你做鲁滨逊,你会选第三型还是第二型的朋友做“礼拜五”呢
    人类最不能伤害的就是自尊
    单靠理论和教训是无济于事的
    交真朋友已是件比较奢侈的事儿
    他一定是一个懂生活、懂人生,爱自己、爱别人的人
    国子监,就是从前的大学
    只有把理想和现实有机结合起来,才有可能成为一个成功之人
  • 原文地址:https://www.cnblogs.com/mayuan01/p/11829565.html
Copyright © 2011-2022 走看看