Arun
Arun

Reputation: 3680

RequestParam value in spring MVC to be case insensitive

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-insensitive.

The method which i use for welcome page is

public String welcome(@RequestParam("orgID") String orgID, ModelMap model)

The request parameter "orgID" should be case insensitive. How to do this ?.

I should be able to give the query-string as "orgid" or "orgId". The parameter should be completely case-insensitive. Looking for your help friends.

Thanks in Advance :-)

Upvotes: 14

Views: 23814

Answers (6)

Aravind A
Aravind A

Reputation: 9697

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 .

Upvotes: 3

Pat
Pat

Reputation: 71

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
}

Upvotes: 2

Ali Alimohammadi
Ali Alimohammadi

Reputation: 194

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;
    }

}

Upvotes: 2

beaudet
beaudet

Reputation: 948

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)
            }
        }
    }

Upvotes: 3

Neo M Hacker
Neo M Hacker

Reputation: 899

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.

Upvotes: 4

randomPerson
randomPerson

Reputation: 116

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
}

Upvotes: 1

Related Questions