异常、类型转换
异常处理器
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="defaultErrorView" value="error"></property> <property name="exceptionAttribute" value="ex"></property> </bean>
这里是在spring的xml文件中写 , 之中使用了视图解析器,error是错误跳转的页面,
<property name="defaultErrorView" value="error"></property>
<property name="exceptionAttribute" value="ex"></property>
这个是异常的对象,el表达式 ,页面使用输出异常对象
2 自定义异常处理
<body> <h2>登录</h2> <form action="/lp" method="post"> 用户名: <input name="username" value="${username}"/> 年龄: <input name="age"/> 出生日期: <input name="birthday"/> <input type="submit" value="登录"/> </form> </body>
@RequestMapping("/login") public String log(String username,int age) throws Exception { System.out.println("adadadda"); if(!username.equals("admin")){ System.out.println("aaa"); throw new NameException("名字错了"); } if(age>60){ System.out.println("bbb"); throw new AgeException("年龄错了"); } return "ax"; }
下面把异常类说一下
public class AgeException extends Exception{ public AgeException() { } public AgeException(String message) { super(message); } }
public class NameException extends Exception { public NameException() { } public NameException(String message) { super(message); } }
这是我们自己的异常
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <!--<property name="defaultErrorView" value="error"></property>--> <property name="exceptionAttribute" value="ex"></property> <property name="exceptionMappings"> <props> <prop key="springmmv.exception.NameException">age</prop> <prop key="springmmv.exception.AgeException">name</prop> </props> </property> </bean>
把异常跳转的页面定义好就行
自定义异常处理器
*/ public class text implements HandlerExceptionResolver { public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) { ModelAndView mv=new ModelAndView(); mv.addObject("ex",e); if(e instanceof NameException){ mv.setViewName("name"); } if(e instanceof AgeException){ mv.setViewName("age"); } return mv; } }
之后在spring中加入一行这个的配置文件就行,就是以他为class的bean
类型转换
Data 的各种处理
<bean id="dg" class="springmmv.cn.news.Conver"></bean> 为转换器的类
<bean id="dg" class="springmmv.cn.news.Conver"></bean> <bean id="jk" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters" ref="dg"></property> </bean> <mvc:annotation-driven conversion-service="jk"/>
public class Conver implements Converter<String,Date> { public Date convert(String str) { SimpleDateFormat sa=gets(str); try { Date date=sa.parse(str); return date; } catch (ParseException e) { e.printStackTrace(); } return null; } public SimpleDateFormat gets(String string){ SimpleDateFormat sm=new SimpleDateFormat("yyyy年MM月dd日"); if(Pattern.matches("^\\d{4}-\\d{2}-\\d{2}",string)){ sm=new SimpleDateFormat("yyyy-MM-dd"); }else if(Pattern.matches("^\\d{4}/\\d{2}/\\d{2}",string)){ sm=new SimpleDateFormat("yyyy/MM/dd"); }else if(Pattern.matches("^\\d{4}\\d{2}\\d{2}",string)){ sm=new SimpleDateFormat("yyyyMMdd"); } return sm; }