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
  • 相关阅读:
    大型网站前端使用图片格式的正确姿势
    移动端开发技术文档
    超详细的Web前端开发规范文档
    try 、catch 、finally 、throw 测试js错误
    ajax大并发问题
    jQuery之Ajax--全局Ajax事件处理器
    如何处理ajax中嵌套一个ajax
    关于for循环里面异步操作的问题
    XMLHttpRequest: 网络错误 0x2f78,…00002f78
    【转载】OGRE中用到的设计模式
  • 原文地址:https://www.cnblogs.com/feng9exe/p/8039238.html
Copyright © 2011-2022 走看看