Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can spring mvc trim all strings obtained from forms?

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
"whatever"
The string has been auto trimmed

Does spring mvc have this function too? THX.

like image 652
Gordian Yuan Avatar asked Apr 22 '10 14:04

Gordian Yuan


People also ask

Why do we use spring forms?

View: Spring MVC form tags are used to render the equivalent HTML form fields, and most importantly, bind the object in the model with the form. Controller: alongside with handling requests, the controller binds the model object with the view and vice-versa, and delegates processing to dedicated business/service class.

What is the method used in spring model to view all the data?

Overview. One of the most important Spring MVC annotations is the @ModelAttribute annotation. @ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute, and then exposes it to a web view.

What is spring mvc4?

Spring MVC is a Model-View-Controller(MVC) web framework build on notion of a central Front Controller servlet (DispatherServlet) which is responsible for dispatching each request to appropriate handlers, resolving views and finally returning the response.


1 Answers

Using Spring 3.2 or greater:

@ControllerAdvice public class ControllerSetup {     @InitBinder     public void initBinder ( WebDataBinder binder )     {         StringTrimmerEditor stringtrimmer = new StringTrimmerEditor(true);         binder.registerCustomEditor(String.class, stringtrimmer);     } } 

Testing with an MVC test context:

@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration public class ControllerSetupTest {     @Autowired     private WebApplicationContext   wac;     private MockMvc                 mockMvc;      @Before     public void setup ( )     {         this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();     }      @Test     public void stringFormatting ( ) throws Exception     {         MockHttpServletRequestBuilder post = post("/test");         // this should be trimmed, but only start and end of string         post.param("test", "     Hallo  Welt   ");         ResultActions result = mockMvc.perform(post);         result.andExpect(view().name("Hallo  Welt"));     }      @Configuration     @EnableWebMvc     static class Config     {         @Bean         TestController testController ( )         {             return new TestController();         }          @Bean         ControllerSetup controllerSetup ( )         {             return new ControllerSetup();         }     } }  /**  * we are testing trimming of strings with it.  *   * @author janning  *   */ @Controller class TestController {     @RequestMapping("/test")     public String test ( String test )     {         return test;     } } 

And - as asked by LppEdd - it works with passwords too as on the server side there is no difference between input[type=password] and input[type=text]

like image 88
Janning Avatar answered Sep 26 '22 22:09

Janning