zoukankan      html  css  js  c++  java
  • SpringMVC(四)@RequestParam

    使用@RequestParam可以将URL中的请求参数,绑定到方法的入参上,并通过@RequestParam的3个参数进行配置

    Modifier and Type Optional Element Description
    String defaultValue 方法入参默认值
    boolean required 是否必须包含该参数,默认为true
    String value 请求参数名

    其实不使用@RequestParam,SpringMVC也会将request的parameter自动绑定到method的parameter中,

    使用@RequestParam只不过是对parametr进行配置,和对URL更精确化的配置

    代码:

       1: @RequestMapping("/testRequestParam")
       2: @Controller
       3: public class TestRequestParam {
       4:     private static final String SUCCESS = "success";
       5:  
       6:     /*
       7:     * 使用@RequestParam绑定入参,并进行配置
       8:     *
       9:     * 当请求为:testRequestParam/user?name=zs&age=200时
      10:     * 输出结果:name = zs class = j1001 age = 200
      11:     *
      12:     * 当请求为:testRequestParam/user?name=zs&age=200&class=j111时
      13:     * 输出结果:name = zs class = j111 age = 200
      14:     * */
      15:     @RequestMapping("/user")
      16:     public String testParam(@RequestParam(value = "name") String name,
      17:                             @RequestParam(value = "class", required = false, defaultValue = "j1001") String cla,
      18:                             Integer age) {
      19:         System.out.println("name = " + name + " class = " + cla + " age = " + age);
      20:         return SUCCESS;
      21:     }
      22: }

    URL:

       1: <a href="testRequestParam/user?name=zs&age=200&class=j111"> name=zs age=200 class="j1111" 输入name、age、class</a>
       2:  
       3: <br/><br/>
       4: <a href="testRequestParam/user?name=zs&age=200"> name=zs age=200 只输入name和age不输入class</a>
       5:  
       6: <br/><br/>
     
    分类: SpringMVC
     
    http://www.cnblogs.com/FFFFF/p/4626756.html
  • 相关阅读:
    updatepanel中不能使用fileupload的弥补方法
    AJAXPro用法,关于JS同步和异步调用后台代码的学习
    How do I get data from a data table in javascript?
    记不住ASP.NET页面生命周期的苦恼
    浅谈ASP.NET中render方法
    解决AjaxPro2中core.ashx 407缺少对象的问题
    ServU 6.0出来了
    关于X Server/Client和RDP的畅想
    这个Blog好像没有分页功能嘛
    AOC的显示器太烂了
  • 原文地址:https://www.cnblogs.com/feng9exe/p/8039238.html
Copyright © 2011-2022 走看看