Can spring mvc trim all strings obtained from forms?

前端 未结 6 1649

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

For example:

I type

\"   whatever \"
in a form and submit, I will get

6条回答
  •  别那么骄傲
    2020-12-13 02:59

    You can also use Spring's conversion service, which has the added benefit of working with and with Spring Webflow. As with the other answers, the major downside is that this is a global change and can't be disabled for certain forms.

    You'll need a converter to do the trimming

    public class StringTrimmingConverter implements Converter {
    
        @Override
        public String convert(String source) {
           return source.trim();
        }
    
    }
    

    Then define a conversion service that knows about your converter.

    
        
        
            
        
        
    
    

    and tie that in to mvc.

    
    

    If you use Spring Webflow then it require a wrapper

     
        
    
    

    and a setting on your flow builder

    
    

提交回复
热议问题