spring异常处理实例(登录例子)

标签: springbeanexceptionclassinputhashmap
2263人阅读 评论(0) 收藏 举报
分类:
给出一个spring异常处理的简单例子:
 spring配置文件Config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
   
<bean id="viewResolver"
        class
="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
<property name="viewClass">
           
<value>
              org.springframework.web.servlet.view.JstlView
           
</value>
        
</property>
        
        
<property name="prefix">
            
<value>/WEB-INF/view/</value>
        
</property>
       
        
<property name="suffix">
           
<value>.jsp</value>
        
</property>
        
   
</bean>
   
   
<bean id="messageSource"
       class
="org.springframework.context.support.ResourceBundleMessageSource">
       
<property name="basename"><value>messages</value></property>
   
</bean>
       
  
   
<bean id="exceptionResolver"
        class
="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        
<property name="defaultErrorView">
           
<value>failure</value>
        
</property>
        
        
<property name="exceptionMappings">
            
<props>
                
<prop key="java.sql.SQLException">showDBError</prop>
                
<prop key="java.lang.RuntimeException">showError</prop>
            
</props>
        
</property>
   
</bean>
   
   
<bean id="RegisterValidator"
       class
="net.xiaxin.validator.RegisterValidator"/>
       
   
<bean id="RegisterAction"
       class
="net.xiaxin.action.RegisterAction">
       
<property name="commandClass">
           
<value>net.xiaxin.reqbean.RegisterInfo</value>
       
</property>
       
       
<property name="validator">
           
<ref local="RegisterValidator"/>
       
</property>
       
       
<property name="formView">
            
<value>register</value>
       
</property>
       
       
<property name="successView">
             
<value>RegisterSuccess</value>
       
</property>
   
</bean>
   
   
<bean id="freemarkerConfig"
       class
="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
       
<property name="templateLoaderPath">
           
<value>WEB-INF/view/</value>
       
</property>
   
</bean>
   
   
<bean id="urlMapping"
        class
="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        
<property name="mappings">
           
<props>
              
<prop key="/login.do">LoginAction</prop>
               
<prop key="/register.do">RegisterAction</prop>
           
</props>
        
</property>
   
</bean>
   
   
<bean id="LoginAction"
       class
="net.xiaxin.action.LoginAction">
       
<property name="commandClass">
          
<value>net.xiaxin.action.LoginInfo</value>
       
</property>
       
       
<property name="fail_view">
           
<value>loginfail</value>
       
</property>
       
       
<property name="success_view">
          
<value>main</value>
       
</property>
   
</bean>
</beans>
登录测试代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>Insert title here</title>
</head>
<body>
   
<form method="post" action="login.do">
        
<align="center">登录</p>
        
<br>
        用户名:
        
<input type="text" name="username">
        
<br>
        密
&nbsp;&nbsp;码:
        
<input type="password" name="password">
        
<br>
        
<p>
           
<input type="submit" value="提交" name="B1">
            
<input type="reset" value="重置" name="B2">
        
</p>
   
</form>
</body>
</html>
loginInfo代码
public class LoginInfo ...{
    
private String username;
    
private String password;
    
public String getUsername() ...{
        
return username;
    }

    
public void setUsername(String username) ...{
        
this.username = username;
    }

    
public String getPassword() ...{
        
return password;
    }

    
public void setPassword(String password) ...{
        
this.password = password;
    }

}


 显示错误页面:showDBError.jsp
<%...@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding
="gb2312"
%>
<%...@ page import="java.lang.Exception" %>
<%...@ page import="java.io.PrintWriter" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Insert title here</title>
</head>
<body>
<%...Exception ex=(Exception)request.getAttribute("exception");%>
<H2>Exception: <%=ex%></H2>
<%...ex.printStackTrace(new java.io.PrintWriter(out));%>
</body>
</html>

LoginAction代码
public class LoginAction extends SimpleFormController...{
     
private String fail_view;
     
private String success_view;
     
    
protected ModelAndView onSubmit(Object cmd,BindException ex)throws Exception...{
        LoginInfo loginInfo
=(LoginInfo)cmd;
        
if(login(loginInfo)==0)...{
            HashMap result_map
=new HashMap();
            result_map.put(
"logininfo", loginInfo);
            
            List msgList
=new LinkedList();
            msgList.add(
"msg1");
            msgList.add(
"msg2");
            msgList.add(
"msg3");
            result_map.put(
"messages", msgList);
            
            
return new ModelAndView(this.getSuccess_view(),result_map);
        }
else...{
            SQLException e
=new SQLException("this is a exception");
        
            
throw e;
//            return new ModelAndView(this.getFail_view());
        }

        
    }

    
    
private int login(LoginInfo loginInfo)...{
        
if("oscar".equalsIgnoreCase(loginInfo.getUsername())&&"123456".equals(loginInfo.getPassword()))...{
            
return 0;
        }

        
return 1;
        
    }

    
public String getFail_view() ...{
        
return fail_view;
    }

    
public void setFail_view(String fail_view) ...{
        
this.fail_view = fail_view;
    }

    
public String getSuccess_view() ...{
        
return success_view;
    }

    
public void setSuccess_view(String success_view) ...{
        
this.success_view = success_view;
    }

}

    
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3403410次
    • 积分:24770
    • 等级:
    • 排名:第250名
    • 原创:448篇
    • 转载:68篇
    • 译文:3篇
    • 评论:319条
    博客专栏
    Ext JS

    文章:44篇

    阅读:205611
    PLM

    文章:28篇

    阅读:50554
    Web Chart

    文章:15篇

    阅读:53019
    最新评论