浏览 2532 次
锁定老帖子 主题:Struts2异常机制
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-09-22
最后修改:2009-09-22
昨天,按Struts2的权威指南,做了一个Sturts2异常处理例子 本人是初学者,流程是理解了,不过如果用到具体项目中,还不知道要什么用 相信在这里有很多有经验的前辈,不吝赐教
1.先搭建一个Struts框架 2.包结构如下:
package lee; public class MyException extends Exception { public MyException() { } public MyException(String msg) { super(msg); } }
package lee; import com.opensymphony.xwork2.Action; public class LoginAction implements Action { private String username; private String password; private String tip; public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void setTip(String tip) { this.tip = tip; } public String getUsername() { return (this.username); } public String getPassword() { return (this.password); } public String getTip() { return tip; } public String execute() throws Exception { if (getUsername().equalsIgnoreCase("user")) { throw new MyException("自定义异常"); } if (getUsername().equalsIgnoreCase("sql")) { throw new java.sql.SQLException("用户名不能为SQL"); } if (getUsername().equals("scott") && getPassword().equals("tiger") ) { setTip("哈哈,服务器提示!"); return SUCCESS; } else { return ERROR; } } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" > <struts> <package name="mypackage" extends="struts-default"> <!-- 此处定义所有的全局结果 --> <global-results> <result name="sql">/exception.jsp</result> <result name="root">/exception.jsp</result> </global-results> <!-- 此处定义全局异常映射 --> <global-exception-mappings> <!-- Action抛出SQLException异常时,转入名为sql的结果 --> <exception-mapping result="sql" exception="java.sql.SQLException"> </exception-mapping> <exception-mapping result="root" exception="java.lang.Exception"> </exception-mapping> </global-exception-mappings> <action name="login" class="lee.LoginAction"> <!-- 下面配置一个局部异常映射,当Action抛出lee.MyException时,转入名为my的结果 --> <exception-mapping result="my" exception="lee.MyException"> </exception-mapping> <!-- 下面定义结果 --> <result name="my">/exception.jsp</result> <result name="success">/welcome.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
<%@ page language="java" contentType="text/html; charset=GBK"%> <html> <head> <title>登录页面</title> </head> <body> <form action="login.action" method="post"> <table align="center"> <caption><h3>用户登录</h3></caption> <tr> <td>用户名:<input type="text" name="username"/></td> </tr> <tr> <td>密 码:<input type="text" name="password"/></td> </tr> <tr align="center"> <td><input type="submit" value="登录"/><input type="reset" value="重填" /></td> </tr> </table> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=GBK"%> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>成功页面</title> </head> <body> 您已经登录! <s:property value="tip"/> </body> </html>
<%@ page language="java" contentType="text/html; charset=GBK"%> <%@taglib prefix="s" uri="/struts-tags"%> <html> <head> <title>异常处理页面</title> </head> <body> <s:property value="exceptionStack"/> </body> </html>
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |