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