1. springMVC类型转换器配置方法
1.1. 自定义类型转换器:将字符串转换为时间
//自定义类型转换器
public class DateConvertor implements Converter<String, Date> {
DateFormat[] dfs = {
new SimpleDateFormat("yyyy-MM-dd"),
new SimpleDateFormat("yyyy/MM/dd"),
new SimpleDateFormat("yyyy.MM.dd") };
@Override
public Date convert(String str) {
for (int i = 0; i < dfs.length; i++) {
try {
return dfs[i].parse(str);
} catch (ParseException ex) {
continue;
}
}
return new Date();
}
}
1.2. 在springmvc-serlvet.xml中注册自定义转换器
<!-- 注入自定义的类型转换器 -->
<bean id="dateConvertor" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.convertor.DateConvertor"></bean>
</set>
</property>
</bean>
<!-- conversion-service 指定自定义转换器在注解中生效 -->
<mvc:annotation-driven conversion-service="dateConvertor"></mvc:annotation-driven>
2. springMVC异常处理
2.1. SimpleMappingExceptionResolver
在springmvc-servlet.xml配置简单映射异常解析器
<!-- 配置简单映射异常解析器 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >
<!-- 指定项目报错后默认的错误页面的视图名 -->
<property name="defaultErrorView" value="error"></property>
<!-- 指定错误页面中用来显示错误信息的变量名 -->
<property name="exceptionAttribute" value="msg"></property>
<!-- 列举可能出现的所有异常的全称
ArithmeticException
IndexOutOfBoundsException
NullPointerException
-->
<property name="exceptionMappings">
<props>
<!-- arith_error 出现ArithmeticException异常,则跳转的目标页面的名称 -->
<prop key="java.lang.ArithmeticException" >arith_error</prop>
<prop key="java.lang.IndexOutOfBoundsException" >index_error</prop>
<prop key="java.lang.NullPointerException" >null_error</prop>
<prop key="com.exception.MyException" >error</prop>
</props>
</property>
</bean>
Arith_error.jsp
<!-- msg是在后台自动将当前的异常对象保存到msg中
对应配置<property name="exceptionAttribute" value="msg"></property>
-->
除零异常:${msg}
UserController代码
@RequestMapping("/f1")
public String fun1() {
int i = 9;
int j = 0;
System.out.println(i / j);
return "index";
}
可以设置自定义异常对象
public class MyException extends Exception {
private String message;
public MyException(String message) {
this.message = message;
}
}
@RequestMapping("/f3")
public String fun3() throws MyException {
int age = 200;
if (age > 0 && age < 120) {
return "index";
} else {
throw new MyException("年龄范围在0-120");
}
}
Error.jsp
<!-- 显示自定义异常的信息 -->
业务异常:${ex.message}
2.2. HandlerExceptionResolver
//自定义的异常处理器:统一处理异常
public class MyExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(
HttpServletRequest request,
HttpServletResponse response,
Object obj,
Exception ex) {
ModelAndView mv=new ModelAndView();
String msg="";
System.out.println(obj);
//获取当前发生的异常对象,根据不同异常,进行不同处理
if (ex instanceof ArithmeticException) {
msg="0不能做除数";
mv.setViewName("arith_error");
}else if (ex instanceof IndexOutOfBoundsException) {
msg="数组索引越界";
mv.setViewName("index_error");
}else if (ex instanceof NullPointerException) {
msg="对象为空";
mv.setViewName("null_error");
}else{
mv.setViewName("error");
}
mv.addObject("msg", msg);
//自定义MyException的信息需要通过属性来显示,将当前异常对象保存在作用域中
mv.addObject("ex", ex);
return mv;
}
}
Springmvc-servlet.xml注册MyExceptionResolver
<bean class="com.exception.MyExceptionResolver"></bean>
2.3.