您的位置:首页 > 编程语言 > Java开发

struts2异常处理

2015-03-28 20:24 369 查看

struts2异常处理,global-results定义全局结果处理

<global-results>定义全局结果处理

一般发生异常之后 结果返回errHandler

因为errHandler是由<global-exception-mappings>关联到Exception这个类了
然后处理结果

<result name="errHandler" type="chain">

然后它就根据

<param name="actionName">errorProcessor</param>

找action

<action name="errorProcessor" class="cn.itcast.sh.error.ErrorProcess">
<result>/error.jsp</result>
</action>
处理了 然后 返回到 error.jsp

在struts.xml中

<?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>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
<!-- <constant name="struts.custom.i18n.resources" value="itcast"></constant> -->
<package name="struts-global" namespace="/" extends="struts-default">
<global-results>
<result name="errHandler" type="chain">
<param name="actionName">errorProcessor</param>
</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="errHandler" exception="java.lang.Exception">
</exception-mapping>
</global-exception-mappings>

<action name="errorProcessor" class="cn.itcast.sh.error.ErrorProcess">
<result>/error.jsp</result>
</action>
</package>


View Code
然后其他包都继承它 就默认使用了其中定义的 错误处理

然后实现 类

class="cn.itcast.sh.error.ErrorProcess"


package cn.itcast.sh.error;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ErrorProcess extends ActionSupport {
private Exception exception;

public Exception getException() {
return exception;
}

public void setException(Exception exception) {
this.exception = exception;
}
@Override
public String execute()
{
ActionContext.getContext().getValueStack().push(this.exception.getMessage());      //放到值栈顶
return this.SUCCESS;
}
}


例子

struts.xml配置

<?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>
<constant name="struts.devMode" value="true" />
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
<!-- <constant name="struts.custom.i18n.resources" value="itcast"></constant> -->
<package name="struts-global" namespace="/" extends="struts-default">
<global-results>
<result name="errHandler" type="chain">
<param name="actionName">errorProcessor</param>
</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="errHandler" exception="java.lang.Exception">
</exception-mapping>
</global-exception-mappings>

<action name="errorProcessor" class="cn.itcast.sh.error.ErrorProcess">
<result>/error.jsp</result>
</action>
</package>
<include file="struts-user.xml"></include>
</struts>


struts-user.xml中


<?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>
<package name="user" namespace="/" extends="struts-global">
<action name="UserAction_*" method="{1}" class="cn.itcast.sh.action.UserAction">
<result name="userList">/user/list.jsp</result>
</action>
</package>
</struts>


然后 如果页面异常 都会转向 error.jsp中 显示

error.jsp可以进行错误显示

因为信息被放到栈顶了 所以可以取到

<s:property />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: