Spring MVC 中的 RequestParam 值不区分大小写

发布于 2024-12-27 19:22:55 字数 313 浏览 1 评论 0原文

我使用查询字符串将数据从 JSP 发送到控制器。

我的控制器是注释驱动的。

请求参数的值应该不区分大小写。

我用于欢迎页面的方法是

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

请求参数“orgID”应该不区分大小写。如何做到这一点?

我应该能够将查询字符串指定为“orgid”或“orgId”。该参数应该完全不区分大小写。寻找您的帮助朋友。

提前致谢 :-)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

指尖上得阳光 2025-01-03 19:22:55

另一种方法是有两个参数“orgId”和“orgid”并具有可选参数。

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

但是,如果您可以控制参数,我强烈希望只采用一种一致的方式,例如 org-id 并在客户端和服务器端都遵循它。

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.

那一片橙海, 2025-01-03 19:22:55

Spring 支持这一点可能会很好,但我也可以理解为什么他们不支持,因为这可能会导致供应商和消费者之间的接口契约减弱。同时,这里有一种相当简单的方法,可以使用参数名称获取第一个值,而不管其大小写。

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

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)
            }
        }
    }
谢绝鈎搭 2025-01-03 19:22:55

您必须尝试更改 Spring 匹配您的 url 的方式。首先,您可以创建一个过滤器(可能是 DelegatingFilterProxyBean)来小写您的参数,然后再将其传递给 Spring 或尝试更改路径匹配的方式。

对第二个选项的解释位于 如何在带有带注释的映射的 Spring MVC 中拥有不区分大小写的 URL

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 .

暮色兮凉城 2025-01-03 19:22:55

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

}

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

}
沙与沫 2025-01-03 19:22:55

我发现的最简单的方法是更改​​控制器方法,以将具有单个键值对的 Map 作为参数,然后将键转换为小写。

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
}

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
}
完美的未来在梦里 2025-01-03 19:22:55

有一个简单的解决方法。您可以直接操作 HttpServletRequest 并使用方法 getParameter() 并检查参数的所有版本。

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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文