使用struts框架处理异常
2015-01-22 00:00
141 查看
struts.xml
RegisterAction.java
error.jsp
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <!-- 指定Struts 2配置文件的根元素 --> <struts> <!-- 指定全局国际化资源文件 --> <constant name="struts.custom.i18n.resources" value="mess"></constant> <!-- 指定国际化编码所使用的字符集 --> <constant name="struts.i18n.encoding" value="GBK"></constant> <!-- 设置常量开启动态调用方法 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <!-- 所有的Action定义都应该放在package下 --> <package name="frank" extends="struts-default"> <!-- 定义全局的结果映射,当局部没找到的情况下就会在全局中找 --> <global-results> <result name="error">error.jsp</result> <result name="exception">error.jsp</result> </global-results> <!-- 全局的异常映射 --> <global-exception-mappings> <!-- 定义一个异常映射,也可以定义局部异常映射 --> <exception-mapping exception="java.lang.Exception" result="exception"></exception-mapping> </global-exception-mappings> <!-- 使用通配符*_* --> <action name="login" class="org.sadhu.app.action.RegistAction"> <!-- 局部的结果映射 --> <result type="dispatcher" name="success"> <param name="location">welcome.jsp</param> </result> </action> </package> </struts>
RegisterAction.java
package org.sadhu.app.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.interceptor.PreResultListener; /* * 使用struts框架处理异常 * * */ public class RegistAction extends ActionSupport { private String userName;//账号 private String password;//密码 private String tip;//提示消息 public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } public String getTip() { return this.tip; } public void setTip(String tip) { this.tip = tip; } public String execute() throws Exception { ActionInvocation invocation = ActionContext.getContext().getActionInvocation(); invocation.addPreResultListener(new PreResultListener() { @Override public void beforeResult(ActionInvocation invocation, String resultCode) { System.out.println("返回的逻辑视图名字为:"+resultCode); //在返回Result之前加入一个额外的数据。 invocation.getInvocationContext().put("extra",new java.util.Date() +"由"+resultCode+"逻辑视图名转入"); } }); if(getUserName().equals("1")) { String s = null; s.toString(); throw new Exception();//抛出一个异常 } if(getUserName().equals("sadhu") && getPassword().equals("sadhu")) { ActionContext.getContext().getSession().put("user", getUserName()); setTip("欢迎,"+getUserName()+",您已经登陆成功!"); return SUCCESS; } else { return ERROR; } } }
error.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <!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=utf-8"> <title>登陆错误</title> </head> <body>登陆错误! <!-- 显示错误消息 --> <s:property value="exception.message" /> <!-- 显示堆栈信息 --> <s:property value="exceptionStack" /> </body> </html>
相关文章推荐
- 使用Application_Error事件处理程序把异常记录到系统事件日志
- 尽可能的使用异常exeption处理
- SQLServer2005中的异常处理消息框可以直接使用
- 我的理解:在有返回值的函数内是否应该使用异常处理?欢迎大家斧正
- PHP5的异常处理机制[5]--PHP5之前的错误处理--使用PEAR处理错误
- Java异常处理机制的详细讲解和使用技巧
- SQL2005中异常处理消息框可直接使用
- java异常处理之使用异常的技巧与原则
- 使用goto 使异常的处理更友善。
- 使用DLL时如何处理“System.NullReferenceException”类型的异常
- 使用BizTalk Server的Sql适配器出现“新事务不能登记到指定的事务处理器中”异常的处理
- Spring.Net AOP 学习之旅: 使用 Throws Advice 处理异常
- 使用 MFC 套接字在静态链接 MFC VisualC++6.0 应用程序, 辅助线程中发生处理异常。
- WebService的异常处理与WebService的Session的使用示例
- 使用 Application_Error 处理异常
- 使用goto 使异常的处理更友善
- 异常处理try...catch(sql 2005版本以上可以使用)
- struts框架中对异常的处理
- 使用Microsoft.ApplicationBlocks.ExceptionManagement处理程序异常
- 使用微软企业库-异常处理