Struts.xml
<package name="ssh2" extends="struts-default">
<global-results>
<result name="sql">/exception.jsp</result>
<result name="root">/exception.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.sql.SQLException" result="sql"/>
<exception-mapping exception="java.lang.Exception" result="root"/>
</global-exception-mappings>
<action name="login" class="loginAction">
<result>/welcome.jsp</result>
<result name="nullPointer">/nullPointer.jsp</result>
<exception-mapping exception="java.lang.NullPointerException" result="nullPointer"/>
</action>
</package>
Action
public class loginAction extends ActionSupport
{
public String add() throws SQLException
{
return "toadd";
}
}
package com.exception ;
public class MyException extends Exception
{
private String message;
public MyException(String message)
{
super(message);
this.message = message ;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
public String execute() throws Exception
{
if(!"hello".equals(usename) || !"world".equals(password))
{
throw new MyException("用户名或密码错误,您发现了吧!");
}
return "success" ;
}
<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="com.struts2.LoginAction">
<exception-mapping result="myex" exception="com.exception.MyException">
</exception-mapping>
<result name="myex">/error.jsp</result>
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
在全局配置中的异常处理
<struts>
<package name="struts2" extends="struts-default">
<global-results>
<result name="myexception1">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="myexception1"
exception="com.exception.MyException">
</exception-mapping>
</global-exception-mappings>
<action name="login" class="com.struts2.LoginAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <body> <!-- 这个exception 是 exception="com.exception.MyException" --> <s:property value="exception.message"/> </body> </html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有