zoukankan      html  css  js  c++  java
  • SpringMVC获取参数的几种方式

    前言:

      年末了,忙了一年了却发现系统的整理的东西很少,一些基础的东西都未做整理,这里就将它随便整理一下,增加一些印象,当然在网上看到一些好的资料也会整理下来以备后用。今天整理一下springMVC获取参数的几种方式。

    正题:

     

    1、直接把表单的参数写在Controller相应的方法的形参中,适用于get方式提交,不适用于post方式提交。

    复制代码
        /**
         * 1.直接把表单的参数写在Controller相应的方法的形参中
          * @param canshu1
         * @param canshu2
         * @return
         */
        @RequestMapping("/demo")
        public String demo(String canshu1,String canshu2) {
            System.out.println("canshu1 is:"+canshu1);
            System.out.println("canshu2 is:"+canshu2);
            return "demo/index";
        }
    复制代码

    url形式:http://localhost:8090/demo/demo?canshu1=111&canshu2=222提交的参数需要和Controller方法中的入参名称一致。

    2、通过HttpServletRequest接收,post方式和get方式都可以。

    复制代码
        /**
         * 2、通过HttpServletRequest接收
          * @param request
         * @return
         */
        @RequestMapping("/demo1")
        public String demo1(HttpServletRequest request) {
            String canshu1=request.getParameter("canshu1");
            String canshu2=request.getParameter("canshu2");
            System.out.println("canshu1is:"+canshu1);
            System.out.println("canshu2is:"+canshu2);
            return "demo/index";
        }
    复制代码

    3、通过一个bean来接收,post方式和get方式都可以。
    (1)建立一个和表单中参数对应的bean

    复制代码
    package demo.model;
    
    public class UserModel {
        
        private String username;
        private String password;
        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;
        }
        
    }
    复制代码

    (2)用这个bean来封装接收的参数

    复制代码
        /**
         * 3、通过一个bean来接收
          * @param user
         * @return
         */
        @RequestMapping("/addUser3")
        public String addUser3(UserModel user) {
            System.out.println("username is:"+user.getUsername());
            System.out.println("password is:"+user.getPassword());
            return "demo/index";
        }
    复制代码

    4、通过@PathVariable获取路径中的参数

    复制代码
        /**
         * 4、通过@PathVariable获取路径中的参数
          * @param username
         * @param password
         * @return
         */
        @RequestMapping(value="/demo2/{username}/{password}",method=RequestMethod.GET)
    public String addUser4(@PathVariable String username,@PathVariable String password) { System.out.println("username is:"+username); System.out.println("password is:"+password); return "demo/index"; }
    复制代码

    例如,访问http://localhost:8090/demo/demo/name/111111 路径时,则自动将URL中模板变量{username}和{password}绑定到通过@PathVariable注解的同名参数上,即入参后username=name、password=111111。
    5、使用@ModelAttribute注解获取POST请求的FORM表单数据
    Jsp表单如下:

    复制代码
    <form action ="<%=request.getContextPath()%>/demo5" method="post"> 
         用户名:&nbsp;<input type="text" name="username"/><br/>
         密&nbsp;&nbsp;码:&nbsp;<input type="password" name="password"/><br/>
         <input type="submit" value="提交"/> 
         <input type="reset" value="重置"/> 
    </form> 
    复制代码

    Java Controller如下:

    复制代码
        /**
         * 5、使用@ModelAttribute注解获取POST请求的FORM表单数据
          * @param user
         * @return
         */
        @RequestMapping(value="/demo5",method=RequestMethod.POST)
        public String demo5(@ModelAttribute("user") UserModel user) {
            System.out.println("username is:"+user.getUsername());
            System.out.println("password is:"+user.getPassword());
            return "demo/index";
        }
    复制代码

    6、用注解@RequestParam绑定请求参数到方法入参

    当请求参数username不存在时会有异常发生,可以通过设置属性required=false解决,例如: @RequestParam(value="username", required=false)

    复制代码
        /**
         * 6、用注解@RequestParam绑定请求参数到方法入参
          * @param username
         * @param password
         * @return
         */
        @RequestMapping(value="/demo6",method=RequestMethod.GET)
        public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {
            System.out.println("username is:"+username);
            System.out.println("password is:"+password);
            return "demo/index";
        }
    复制代码
  • 相关阅读:
    Mysql登录错误:ERROR 1045 (28000): Plugin caching_sha2_password could not be loaded
    Docker配置LNMP环境
    Docker安装mysqli扩展和gd扩展
    Docker常用命令
    Ubuntu常用命令
    单例模式的优缺点和使用场景
    ABP 多租户数据共享
    ABP Core 后台Angular+Ng-Zorro 图片上传
    ERROR Error: If ngModel is used within a form tag, either the name attribute must be set or the form control must be defined as 'standalone' in ngModelOptions.
    AbpCore 执行迁移文件生成数据库报错 Could not find root folder of the web project!
  • 原文地址:https://www.cnblogs.com/zhangdiIT/p/8193369.html
Copyright © 2011-2022 走看看