Struts1.X声明式异常

1.编写相应的异常类;
2.捕捉相应的异常类,进行抛出,可以把该代码封装到相应的方法中,比如在LoginDAO这个类中,有个isLogin()方法判断是否登陆成功,同时根据需要抛出相应异常,如:UserNotFoundException、PasswordErrorException等。在LoginAction这个Action类中调用LoginDAO类中的isLogin()方面验证用户是否登陆成功;
3.在国际化资源文件中,编写相应异常的key,以及对于的value值;

4.在struts-config.xml中配置以上相关需要的信息,同时,配置<exception/>标签,指定相应的key,type的属性,key值对应国际化资源文件中异常的key值,type为自己编写的异常类。同时,在相应Action中配置input属性,该属性为出现异常时进行跳转的页面。如在<exception/>中配置path属性,则path属性优先于<action/>中的input属性。

5.在需要显示错误提示的jsp页面用<html:errors/>标签进行错误信息读取。

 

LoginDAO类:

Java代码
  1. public class LoginDAO {   
  2.     public void isLogin(String username,String password){   
  3.         if(!(username.equals("admin"))){   
  4.             throw <SPAN style="COLOR: #ff0000">new UserNotFoundException();</SPAN>   
  5. //事先声明的异常类   
  6.         }   
  7.         else if(!(password.equals("admin"))){   
  8.             throw <SPAN style="COLOR: #ff0000">new PasswordErrorException()</SPAN>   
  9. ;//事先声明的异常类   
  10.         }   
  11.     }   
  12. }   
  13.   
  14. <SPAN style="COLOR: #ff0000"><STRONG>LoginAction类:</STRONG>   
  15. </SPAN>   
  16.   
  17. package wiki.struts;     
  18.   
  19. import javax.servlet.http.HttpServletRequest;   
  20. import javax.servlet.http.HttpServletResponse;   
  21. import org.apache.struts.action.Action;   
  22. import org.apache.struts.action.ActionForm;   
  23. import org.apache.struts.action.ActionForward;   
  24. import org.apache.struts.action.ActionMapping;   
  25. import org.apache.struts.action.DynaActionForm;   
  26.   
  27. public class LoginAction extends Action {   
  28.   
  29.     @Override  
  30.     public ActionForward execute(ActionMapping mapping, ActionForm form,   
  31.             HttpServletRequest request, HttpServletResponse response)   
  32.             throws Exception {   
  33.         DynaActionForm daf = (DynaActionForm)form;   
  34.         String username = (String)daf.get("username");   
  35.         String password  = (String)daf.get("password");   
  36.         LoginDAO loginDAO = new LoginDAO();   
  37.         loginDAO.isLogin(username, password);   
  38.         return mapping.findForward("success");   
  39.     }   
  40. }    
  41.     

 

struts-config.xml中的部分配置

Xml代码
  1. <action-mappings>  
  2.         <action path="/login" type="wiki.struts.LoginAction"  
  3.             name="loginForm" scope="request" <SPAN style="COLOR: #ff0000">input="/login.jsp"</SPAN>  
  4. >  
  5.             <SPAN style="COLOR: #ff0000"><exception key="login.user.name.error"  
  6.                 type="wiki.struts.UserNotFoundException"   path="/login_error.jsp" />  
  7.             <exception key="login.user.password.error"  
  8.                 type="wiki.struts.PasswordErrorException" path="/login_error.jsp" /></SPAN>  
  9.   
  10.             <forward name="success" path="/login_success.jsp" />  
  11.         </action>  
<script type="text/javascript"><!--
google_ad_client = "pub-8638528334131919";
/* 728x90, 创建于 09-3-18 */
google_ad_slot = "1046175043";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

posted on 2009-03-26 00:10 MichaelLee 阅读(440) 评论(0)  编辑  收藏 所属分类: Struts


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

公告

====Michael Lee====
=Java Sofware Engineer=
=Work @ Beijing=
---再烦,也别忘微笑;再急,也要注意语气;再苦,也别忘坚持;再累,也要爱自己!---
---低调做人,你会一次比一次稳健;高调做事,你会一次比一次优秀---
---成功的时候不要忘记过去;失败的时候不要忘记还有未来---

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜