本文介绍了春天的mvc。不区分大小写的get参数映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我尝试编写代码:

pojo:

class MyBean{

    public String getValueName() {
        return valueName;
    }

    public void setValueName(String valueName) {
        this.valueName = valueName;
    }

    String valueName;
}

控制器内部:

    @ModelAttribute
    public MyBean createMyBean() {
        return new MyBean();
    }
    @RequestMapping(value = "/getMyBean", method = RequestMethod.GET)
    public String getMyBean(@ModelAttribute MyBean myBean) {
        System.out.println(myBean.getValueName());
        return "pathToJsp";
    }

web.xml配置:

web.xml configuration:

<filter>
    <filter-name>caseInsensitiveFilter</filter-name>
    <filter-class>com.terminal.interceptor.CaseInsensitiveRequestFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>caseInsensitiveFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

过滤器:

@Component
public class CaseInsensitiveRequestFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        filterChain.doFilter(new CaseInsensitiveHttpServletRequestWrapper(request), response);
    }

    private static class CaseInsensitiveHttpServletRequestWrapper extends HttpServletRequestWrapper {

        private final LinkedCaseInsensitiveMap params = new LinkedCaseInsensitiveMap();

        /**
         * Constructs a request object wrapping the given request.
         *
         * @param request
         * @throws IllegalArgumentException if the request is null
         */
        private CaseInsensitiveHttpServletRequestWrapper(HttpServletRequest request) {
            super(request);
            params.putAll(request.getParameterMap());
        }

        @Override
        public String getParameter(String name) {
            String[] values = getParameterValues(name);
            if (values == null || values.length == 0) {
                return null;
            }
            return values[0];
        }

        @Override
        public Map getParameterMap() {
            return Collections.unmodifiableMap(this.params);
        }

        @Override
        public Enumeration getParameterNames() {
            return Collections.enumeration(this.params.keySet());
        }

        @Override
        public String[] getParameterValues(String name) {
            return (String[]) params.get(name);
        }
    }
}

在调试中我看到过滤器方法调用,但我无法实现case insentive get参数映射。

In debug I see that filter method invoke but I cannot achieve case insentive get parameters mapping.

例如 localhost:8081 / getMyBean?valueName = trololo 工作但 localhost:8081 / getMyBean?valuename = trololo - 不是

for example localhost:8081/getMyBean?valueName=trololo works but localhost:8081/getMyBean?valuename=trololo - not

推荐答案

我相信你的问题是 @ModelAttribute 。您要求Spring将参数映射到 MyBean 对象,并且此对象中的属性为 valueName

Your problem, I believe, is @ModelAttribute. You're asking Spring to map the parameters to MyBean object, and the property inside this object is valueName.

为了将Spring反映到对象的params,它需要在正确的情况下。

In order Spring to reflect the params to the object, it needs to be in the correct case.

您有2个选项:


  • 在MyBean对象中将属性名称更改为 valuename ,以及所有属性名称为小写,它应该在您的解决方案的帮助下工作。

  • 删除 @ModelAttribute 并放入每个属性的@RequestParam 。如果你有很多道具,这可能会很痛苦。

  • change the property name to valuename in your MyBean object, and all property names to lowercase, it should work with help of your solution.
  • remove @ModelAttribute and put @RequestParam for each property. If you have many props, this could be painful.

这篇关于春天的mvc。不区分大小写的get参数映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 23:24