views:

146

answers:

1

I know struts2 default config will trim all string obtained from forms.

For example.

In form I type " whatever " in form and submit finally I will get "whatever". It's will auto trim the string.

I want to know is that spring mvc have this function too? THX.

+1  A: 

You can user a Spring-MVC Interceptor

public class TrimInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Enumeration<String> e = request.getParameterNames();
        while(e.hasMoreElements()) {
            String parameterName = e.nextElement();

            request.setParameter(parameterName, request.getParameter(parameterName).trim());
        }

        return true;
    }

And set up your HandlerMapping interceptors property

<bean id="interceptorTrim" class="br.com.view.interceptor.TrimInterceptor"/>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" p:interceptors-ref="interceptorTrim"/>
}

Or use a Servlet Filter

Arthur Ronald F D Garcia
Be wary of an approach such as this - a user may use a space as the first and/or last character of their password. Your implementation above also is not null safe.
anger