according this answer I try to write my code:
pojo:
class MyBean{
public String getValueName() {
return valueName;
}
public void setValueName(String valueName) {
this.valueName = valueName;
}
String valueName;
}
inside controller:
@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 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>
Filter:
@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);
}
}
}
In debug I see that filter method invoke but I cannot achieve case insentive get parameters mapping.
for example localhost:8081/getMyBean?valueName=trololo
works but localhost:8081/getMyBean?valuename=trololo
- not
Filter
should be invoked? – Dielle@ModelAttribute
mapping? – Adder