zoukankan      html  css  js  c++  java
  • Spring3 MVC请求参数获取的几种场景

     访问/aaa/bbb所对应的@Controller

    @RequestMapping("/aaa")//类级别,可以不需要,如果要了,下面所有的请求路径前都需要加入/aaa   
     public class DefaultController{    
        
          @RequestMapping("/bbb")
                  //方法级别,必须有,决定这个方法处理哪个请求,如果有类级别 /aaa/bbb
          public String xxx()    
          {    
                //如果没有类级别的就直接请求/bbb   
                return;    
          }    
    }

    一、通过@PathVariabl获取路径中的参数

    eg1:
    例如,访问user/123/chan路径时,执行以上方法,其中,参数id=123,name=chan
    @Controller使用以下代码来接收参数

    @RequestMapping(value="user/{id}/{name}",method=RequestMethod.GET)
        public String printMessage1(@PathVariable String id,@PathVariable String name, ModelMap model) {
            
            System.out.println(id);
            System.out.println(name);
            model.addAttribute("message", "111111");
            return "users";
        }

     eg2:

    @RequestMapping("/somepath/{userName}")
    public String register(@PathVariable(value = "userName") String userName) {
    ModelAndView mav = new ModelAndView();
    return "user/createSuccess";
    }
    
    
    在springmvc注解的@RequestMapping("/somepath/{userName}")如何处理默认值的情况,比如我在发送请求的时候,userName有时候是没有的,所以导到了不能匹配这个action
    http://localhost:8080/spc/movie/somepath/就不能匹配上面的请求:
    http://localhost:8080/spc/movie/somepath/tom而这个才可以匹配。
    
    
    @PathVariable 会将url中的参数解析到对应的方法参数上,需要在@RequestMapping()指定匹配模式
    @RequestMapping("somepath/{userName}")
    这时你访问地址"somepath/Tom"就能把"Tom"解析到方法参数userName上

    http://blog.sina.com.cn/s/blog_beb1f2ec01015pz4.html

    @RequestMapping("/user")或@RequestMapping(value="/user")  对路径进行映射 
    @RequestMapping(params="user")  对参数进行映射 
    @RequestParam(value="user") 对参数进行映射 

    二、用注解@RequestParam绑定请求参数

    例如,访问user?name=chan路径时
    @Controller如下

    @RequestMapping(value = "/user", method = RequestMethod.GET) 
    public String setupForm(@RequestParam("name") String name, ModelMap model) { 
       System.out.println(name); 
    return "helloWorld";
    }

    用注解@RequestParam绑定请求参数name到变量name

    当请求参数a不存在时会有异常发生,可以通过设置属性required=false解决,

    例如: @RequestParam(value="name", required=false)

    请求方法映射
    
    或 的关系:method={RequestMethod.POST, RequestMethod.GET};
    
    如果需要支持OPTIONS、TRACE,请添加DispatcherServlet在web.xml的初始化参数:dispatchOptionsRequest 和 dispatchTraceRequest 为true。
    
    请求参数映射
    
    @RequestMapping(params="create") 有create参数
    
    @RequestMapping(params!="create") 没有create参数
    
    @RequestMapping(params="create=sfp") create=sfp
    
    @RequestMapping(params="create!=sfp") create!=sfp
    
    且 的关系 @RequestMapping(params={"test1", "test2=create"})

    http://www.cnblogs.com/wang-jing/p/4567214.html


    三、@ModelAttribute获取POST请求的FORM表单数据
    提交以下表单

    <form method="post" action="user">
        a: <input id="a" type="text"   name="a"/>
        b: <input id="b" type="text"   name="b"/>
        <input type="submit" value="Submit" />
     </form>

    Java  Pojo如下

    public class Pojo{
            private String a;
            private int b;
        }

    Java @Controller如下

    @RequestMapping(value = "/user",method = RequestMethod.POST) 
    public String processSubmit(@ModelAttribute("pojo") Pojo pojo) {  
        return "helloWorld"; 
    }



    三、直接用HttpServletRequest获取

    例如,访问user?name=chan路径时
    @Controller如下

    @RequestMapping(value = "/user",method = RequestMethod.GET) 
    public String get(HttpServletRequest request, HttpServletResponse response) { 
       System.out.println(request.getParameter("name")); 
        return "helloWorld"; 
    }




    http://www.cnblogs.com/leiOOlei/p/3658147.html










  • 相关阅读:
    抽取一个简单的按钮方法
    一些iOS笔试题目
    使用第三方框架 Masonry 实现自动布局
    AutoLayout适配
    iOS面试小题集锦
    大牛们的技术博客
    5、过滤流
    3、过滤文件夹
    1、File类简介
    贪婪模式和非贪婪模式
  • 原文地址:https://www.cnblogs.com/softidea/p/4570805.html
Copyright © 2011-2022 走看看