异常处理后,能够提供友好的用户界面,而不是一些错误的代码信息.
利用struts2的异常处理机制可以很方便的实现异常处理,你不再
需要在Action中捕获异常,并抛出相关的异常了,这些都交给拦截器来帮你做了。在struts-default.xml中己经设置了拦截器,因此我们
可以直接使用声明式异常.struts2-default-xml声明的拦截器如下:
<interceptors>
<interceptor name="exception"
class="com.opensymphony.xwork.interceptor.ExceptionMappingInterceptor"/>
</interceptors>
异常的类型可以分为两种:局部异常映射和全局异常映射
当Action抛出异常时,会在局部中和全局中查找与之相匹配的异常,如果局部和全局中都有满足的异常映射,以局部的为准. 例子说明:
action:
public class LogonAction {
private String userName;
private String userPwd;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String execute() throws Exception{
if(userName.equals("callan") && userPwd.equals("fjf")){
ActionContext.getContext().getSession().put("userName", "callan");
return "succee";
} else {
throw new SQLException(“用户名密码不正确”);
}
}
}
配置文件:
<package name="logon" extends="struts-default">
<!-- global-exception-mappings必需在action的上面 -->
<global-exception-mappings>
<exception-mapping result="all" exception="java.lang.Exception">
</exception-mapping>
</global-exception-mappings>
<action name="logon" class="com.LogonAction">
<result name="succee">/logon/welcome.jsp</result>
<result name="error">/logon/error.jsp</result>
<exception-mapping result="sql" exception="java.sql.SQLException"/></exception-mapping>
<exception-mapping result="nullPoint" exception="java.lang.NullPointerException"></exception-mapping>
<result name="nullPoint">/logon/nullpoint.jsp</result>
<result name="sql">/logon/sql.jsp</result>
<result name="all">/logon/all.jsp</result>
</action>
</package>
上面是个用户登陆的action及配置,定义了两个局部异常和一个全局异常
当输入的用户名和密码不为callan和fjf时,会抛出SQLException异常,局部异常配置起作用,会定向到sql.jsp,如果注释局部sql异常,全局异常起作用,定向到all.jsp
可以使用Struts2的标签来输出异常信息
<s:property value="exception.message"/>
<s:property value="exceptionStack"/>
例如sql.jsp页面
<body>
<s:property value="exception.message"/>
</body>
可以输出 用户名密码不正确
分享到:
相关推荐
NULL 博文链接:https://yuxisanren.iteye.com/blog/1707508
Struts2异常处理机制
主要介绍了Struts2中异常处理机制分析,涉及到了声明式异常捕捉的相关内容,以及两种异常映射的分析,需要的朋友可以参考下。
struts2的异常处理,解决异步请求时,异常以json格式发送到客户端。以及dao单元测试的配置。
配合博文:http://blog.csdn.net/u011638419/article/details/41343683
本文档主要介绍了struts2的异常处理机制,希望对你有所帮助。
项目中出现的异常通常要用一个友好的异常页面来显示,通过对struts2.xml的配置能拦截全局异常,只要出现异常就会转向异常页面。
Struts2的异常处理实例 博文链接:https://zmx.iteye.com/blog/463996
Struts2 异常处理的四种获取属性方法,大家可以借鉴学习
NULL 博文链接:https://wjt276.iteye.com/blog/690323
NULL 博文链接:https://wlh269.iteye.com/blog/321746
struts常见异常及处理
struts2 单文件,多文件上传及异常处理
从网上搜索的一段关于struts1.x自定义异常处理的方法。
在struts2中使用result里type="stream"的结果类型时,可以实现文件的下载管理,使用时也是比较顺畅,但是当在“下载提示窗口”中点击“取消按钮”时,总是报出“java.lang.IllegalStateException”异常,异常内容...
《Struts 2完全学习手册》全面讲述Struts 2开发框架的知识,包括Struts 2中配置文件的详细讲解、国际化与异常处理、数据类型的转换、数据的输入校验、Struts 2强大的标签库、Struts 2中文件的上传与下载、Struts 2对...
struts2异常处理实例
警告: 本工具为漏洞自查工具,请勿非法攻击他人网站! Struts2漏洞检查工具2017版 V2.0 ...jakatar处理复杂数据类型时,异常处理不当,导致OGNL代码执行,通过在请求的Content-Type头中构造OGNL表达式来执行Java代码。
本文档是写的Java相关的异常及其处理办法 包括:Java、Jdbc、Android、JavaScript、Servlet、Jsp、Hibernate、Struts2中一般出现的异常及其解决办法,注明适用于初学者。
NULL 博文链接:https://xace.iteye.com/blog/416561