• SpringMVC探究-----常用获取传递参数的方法


       1.@RequestParam

         @RequestParam 常用来映射请求参数,它有三个属性可以配置:

    • value 值即请求参数的参数名
    • required 该参数是否必须. 默认为 true
    • defaultValue 请求参数的默认值
    <a href="springmvc/testRequestParam?username=yyx&age=29">Test
            RequestParam</a>
    @RequestMapping(value = "/testRequestParam")
        public String testRequestParam(@RequestParam(value = "username") String username,
                @RequestParam(value = "age", required = false, defaultValue = "0") int age) {
            // @RequestParam(value = "age", required = false) Integer age) {
            System.out.println("testRequestParam,username:" + username + ",age:" + age);
            return SUCCESS;
        }

        注意:Integer值可以为null,int存入时必须有值

       2.@PathVariable

         @PathVariable 可以来映射 URL 中的占位符到目标方法的参数中.体现restful风格

    <a href="springmvc/testPathVariable/1">Test PathVariable</a>
    @RequestMapping("/testPathVariable/{id}")
        public String testPathVariable(@PathVariable("id") Integer id) {
            System.out.println("testPathVariable:" + id);
            return SUCCESS;
        }

       3.HttpServletRequest

         HttpServletRequest的getParameter(参数String)方法获取请求中的参数

    <a href="springmvc/testServletAPI?userName=Tom">Test ServletAPI</a>
    @RequestMapping("/testServletAPI")
        public String testServletAPI(HttpServletRequest request)
                throws IOException {
            System.out.println(request.getParameter("userName"));
            return SUCCESS;
        }

         注意:参数名称必须和登陆页面的name="userName"相同

       4.直接把表单的参数写在Controller相应的方法的形参中

    <form action="${pageContext.request.contextPath}/springmvc/login"
            method="post">
            <table>
                <tr>
                    <td>用户名:</td>
                    <td><input type="text" name="userName"></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><input type="password" name="userPwd"></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="登陆"></td>
                </tr>
            </table>
        </form>
    /**
         * 控制器中方法的形参userName,userPwd
         * 名称必须和登陆页面的name="userName",name="userPwd"相同
         * @param userName
         * @param userPwd
         * @return
         */
        @RequestMapping("/login")
        public String loginByUser(String userName, String userPwd) {
            System.out.println("userName is:" + userName);
            System.out.println("userPwd is:" + userPwd);
            return "success";
        }

      注意: name="userName"和String userName要一一对应,名称相同

       5.通过一个bean来接收

    package com.springmvc.entity;
    
    public class User {
        private String userName;
        private String userPwd;
    
        public User() {
            super();
        }
    
        public User(String userName, String userPwd) {
            super();
            this.userName = userName;
            this.userPwd = userPwd;
        }
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getUserPwd() {
            return userPwd;
        }
    
        public void setUserPwd(String userPwd) {
            this.userPwd = userPwd;
        }
    
        @Override
        public String toString() {
            return "User [userName=" + userName + ", userPwd=" + userPwd + "]";
        }
    
    }
    <form action="${pageContext.request.contextPath}/springmvc/loginPoJo"
            method="post">
            <table>
                <tr>
                    <td>用户名:</td>
                    <td><input type="text" name="userName"></td>
                </tr>
                <tr>
                    <td>密码:</td>
                    <td><input type="password" name="userPwd"></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="登陆"></td>
                </tr>
            </table>
        </form>
    /**
         *  登陆页面的name="userName",name="userPwd"必须和Bean类的属性名称相同
         * @param user
         * @return
         */
        @RequestMapping("/loginPoJo")
        public String loginByUser(User user) {
            System.out.println("userName is:" + user.getUserName());
            System.out.println("userPwd is:" + user.getUserPwd());
            return "success";
        }

       注意: name="userName"和PoJo属性要一一对应,名称相同

  • 相关阅读:
    WinDbg调试C#技巧,解决CPU过高、死锁、内存爆满
    Window环境下搭建Git服务器
    Virtual Box虚拟机Ubuntu系统安装及基本配置
    修改VS2017新建类模板文件添加注释
    .net core 使用IIS作为宿主Web服务器,部署常见问题
    Asp.Net进程外Session(状态服务器Session、数据库Session)
    百度地图DEMO-路线导航,测距,标点
    c#文件图片操作
    C#代码安装Windows服务(控制台应用集成Windows服务)
    通过经纬度获取地址信息
  • 原文地址:https://www.cnblogs.com/fengfuwanliu/p/10293869.html
Copyright © 2020-2023  润新知