RequestParam value in spring MVC to be case insensitive

前端 未结 6 2106
离开以前
离开以前 2021-01-17 20:40

Am sending data from JSP to controller using query string.

My controller is annotation driven.

The value of the the request parameter should be case-insensit

相关标签:
6条回答
  • 2021-01-17 21:03

    The simplest way I found was to change the controller method to take a Map with a single key value pair as a parameter and then convert the key to lowercase.

    public String welcome(@RequestParam(required = true) Map<String, String> params, ModelMap model) {
       String caseInsensitiveOrgId = params.keySet().toArray()[0].toString().toLowerCase();
       String OrgValue = params.values().toArray()[0];
       // Do whatever you want here
    }
    
    0 讨论(0)
  • 2021-01-17 21:06

    Another approach would be to have two parameters "orgId" and "orgid" and have the optional.

    public String welcome(@RequestParam(value="orgID", required = false) String org_ID, @RequestParam(value="orgid", required=false, String orgid, ModelMap model) {
    final String orgId = orgid == null ? org_ID : orgid;
    ...
    }
    

    But if you have control over the parameters I would strongly prefer to just have one consistent way, say org-id and follow it both in the client and the server side.

    0 讨论(0)
  • 2021-01-17 21:13

    in Spring 4.2+

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages="org.saat")
    public class WebConfig extends WebMvcConfigurerAdapter {
    
        @Override
        public void configurePathMatch(PathMatchConfigurer configurer) {
            AntPathMatcher matcher = new AntPathMatcher();
            matcher.setCaseSensitive(false);
            configurer.setPathMatcher(matcher);
        }
    
        @Bean
        public ViewResolver viewResolver() {
            InternalResourceViewResolver viewResolver = new 
    InternalResourceViewResolver();
            viewResolver.setPrefix("/");
            viewResolver.setSuffix(".jsp");
            return viewResolver;
        }
    
    }
    
    0 讨论(0)
  • 2021-01-17 21:20

    It might be nice for Spring to support this, but I can also understand why they wouldn't since it could result in a weakening of an interface contract between a supplier and consumer. In the meantime, here's a fairly straightforward way to take the first value using the parameter name regardless of its case.

        String myVal = null;
        for (String pname : request.getParameterMap().keySet() ) {
            if ( pname != null && pname.toLowerCase().equals("your_lc_pname") ) {
                for (String v : request.getParameterValues(pname) ) {
                    // do something with your value(s)
                }
            }
        }
    
    0 讨论(0)
  • 2021-01-17 21:21

    There is a simple workaround.You can operate directly on the HttpServletRequest and use method getParameter() and check all versions of parameter.

    public String welcome(HttpServletRequest request, ModelMap model){
       String orgID = extractOrgId(request);
       //rest of your code
    }
    
    private String extractOrgId(HttpServletRequest request){
       if(request.getParameter("orgId") != null){
           return request.getParameter("orgId");
       }
       // and so on
    }
    
    0 讨论(0)
  • 2021-01-17 21:21

    You'll have to try changing the way Spring matches your urls . You could for one, create a filter (probably a DelegatingFilterProxyBean) to lower case your parameter before you pass it on to Spring or try to change the way the paths are matched .

    An explanation to the second options is given at How can I have case insensitive URLS in Spring MVC with annotated mappings .

    0 讨论(0)
提交回复
热议问题