Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 610
Arun Avatar asked Jan 19 '12 07:01

Arun


People also ask

Is RequestParam case sensitive?

The parameter should be completely case-insensitive.

Is RequestParam encoded?

Encoded vs Exact Value On the other hand, @RequestParam is encoded.

Is RequestParam required by default?

Method parameters annotated with @RequestParam are required by default. will correctly invoke the method. When the parameter isn't specified, the method parameter is bound to null.

What is the difference between QueryParam and RequestParam?

@QueryParam is a JAX-RS framework annotation and @RequestParam is from Spring. QueryParam is from another framework and you are mentioning Spring. @Flao wrote that @RequestParam is from Spring and that should be used in Spring MVC.


2 Answers

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)
            }
        }
    }
like image 195
beaudet Avatar answered Sep 19 '22 14:09

beaudet


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.

like image 22
Neo M Hacker Avatar answered Sep 21 '22 14:09

Neo M Hacker