spring3之前,spring web不人性化的复杂配置,一直是spring web的诟病,特别是验证这一块,而annotation的配置也不甚灵活,而在javaeye上看到了spring3的Validation的一个片段已经spring3关于spring web最新的文档,我觉得struts2是应该到了放弃的时候了。
public class Person {
- @NotNull
- @Max(64)
- private String name;
- @Min(0)
- private int age;
- }
public class Person { @NotNull @Max(64) private String name; @Min(0) private int age;}
@Controller
- public class MyController {
- @InitBinder
- protected void initBinder(WebDataBinder binder) {
- binder.setValidator(new FooValidator());
- }
- Spring Framework 3.0.0.RC1 Reference Documentation 143
- @RequestMapping("/foo", method=RequestMethod.POST)
- public void processFoo(@Valid Foo foo) { ... }
- }
@Controllerpublic class MyController { @InitBinder protected void initBinder(WebDataBinder binder) { binder.setValidator(new FooValidator()); } Spring Framework 3.0.0.RC1 Reference Documentation 143 @RequestMapping("/foo", method=RequestMethod.POST) public void processFoo(@Valid Foo foo) { ... }}
- <!-- Invokes Spring MVC @Controller methods -->
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
- <property name="webBindingInitializer">
- <!-- Configures Spring MVC DataBinder instances -->
- <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
- <property name="validator" ref="validator" />
- </bean>
- </property>
- </bean>
- <!-- Creates the JSR-303 Validator -->
- <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />