异常、类型转换

   异常处理器 

 

 <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;

    }
posted @ 2018-04-02 15:03  我不是.好人  阅读(220)  评论(0编辑  收藏  举报