验证
导入jar包
写提示文件(ValidataMessages.properties)
user_username_empty=\u8D26\u53F7\u4E0D\u80FD\u4E3A\u7A7A
user_username_size=\u8D26\u53F7\u957F\u5EA6\u4E3A3~6
user_password_empty= \u5BC6\u7801\u4E0D\u80FD\u4E3A\u7A7A
user_password_size=\u5BC6\u7801\u957F\u5EA63~6
xml配置文件
<!-- 开启扫描 -->
<context:component-scan base-package="com.liy.controller"></context:component-scan>
<!-- 开启mvc注解 -->
<mvc:annotation-driven validator="validator"></mvc:annotation-driven>
<!--添加对JSR-303验证框架的支持 -->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<!--不设置则默认为classpath下的 ValidationMessages.properties -->
<property name="validationMessageSource" ref="validatemessageSource"/>
</bean>
<bean id="validatemessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:ValidataMessages"/>
<property name="fileEncodings" value="utf-8"/>
<property name="cacheSeconds" value="120"/>
</bean>
</beans>
javabean层
public class User {
public Integer id;
@NotBlank(message="{user_username_empty}",groups={AvlidateGroup1.class})
@Size(max=6,min=3,message="{user_username_size}",groups={AvlidateGroup2.class})
public String username;
@NotBlank(message="{user_password_empty}",groups={AvlidateGroup1.class})
@Size(max=6,min=3,message="{user_password_size}",groups={AvlidateGroup2.class})
public String password;
public User() {
super();
}
public User(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
controller控制层
@Controller
public class Validata {
@RequestMapping("/validata")
@ResponseBody
public void validata1(@Validated(value={AvlidateGroup1.class}) User user,BindingResult br){
System.out.println(user);
List<ObjectError> objerr = br.getAllErrors();
if (objerr != null || objerr.size() >0) {
for (ObjectError objectError : objerr) {
System.out.println(objectError.getDefaultMessage());
}
}else {
System.out.println(user);
}
}
@RequestMapping("/validata1")
@ResponseBody
public void validata2(@Validated(value={AvlidateGroup2.class}) User user,BindingResult br){
System.out.println(user);
List<ObjectError> objerr = br.getAllErrors();
if (objerr != null || objerr.size() >0) {
for (ObjectError objectError : objerr) {
System.out.println(objectError.getDefaultMessage());
}
}else {
System.out.println(user);
}
}
分组的用的接口(里面不用写内容 , 就是用来区分一个属性的多个验证信息)
发生错误 ,跳转到错误页面
@Component
public class MyHandlerExceptionResolver implements HandlerExceptionResolver{
@Override
public ModelAndView resolveException(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
Exception arg3) {
System.out.println("出现异常了--"+arg3.getMessage());
ModelAndView mv = new ModelAndView();
mv.addObject("exe", arg3.getMessage());
mv.setViewName("error");
return mv;
}
}
配置文件
<!-- 开启扫描 -->
<context:component-scan base-package="com.controller,com.liy.*"></context:component-scan>
<!-- 开启mvc注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 给返回的字符串加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
json的转换(spring自带的就只要导入jar包就可以了 无需配置)
导入jar包
controller层代码
@Controller
public class MyController {
//响应返回数据转换成json只要加上@responseBody注解
//接收请求的数据转换json加上@requestBody注解
@RequestMapping("/fun")
@ResponseBody
public void fun(@RequestBody User user){
System.out.println(user);
}
使用其他的json转换的jar包
比如fastJSON
导入jar包
然后在配置文件里配置
<!-- 开启mvc注解 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"></bean>
</mvc:message-converters>
</mvc:annotation-driven>
springmvc中,直接接收json参数,如果参数中有日期的话,不需要定义日期类型转换器,日期的转换由gson/jackson/fastjson来提供。