后台抛出自定义异常
1 public String execute() throws Exception { 2 3 if (getUser().equalsIgnoreCase("user")) 4 { 5 throw new ZcxException("自定义异常"); 6 } 7 if (getUser().equalsIgnoreCase("sql")) 8 { 9 throw new java.sql.SQLException("用户名不能为SQL"); 10 } 11 if (getUser().equals("zcx")) 12 { 13 addActionMessage("服务器提示!"); 14 return SUCCESS; 15 } 16 return ERROR; 17 }
struts配置异常处理
1 2 3 <package name="zcx.controller" namespace="/" extends="struts-default"> 4 <!-- 定义全局结果映射 --> 5 <global-results> 6 <!-- 定义当sql、root两个逻辑异常都对应exception.jsp页 --> 7 <result name="sql">/WEB-INF/content/exception.jsp</result> 8 <result name="root">/WEB-INF/content/exception.jsp</result> 9 </global-results> 10 11 <!-- 定义全局异常映射 --> 12 <global-exception-mappings> 13 <!-- 当Action中遇到SQLException异常时, 14 系统将转入name为sql的结果中--> 15 <exception-mapping exception="java.sql.SQLException" result="sql"/> 16 <!-- 当Action中遇到Exception异常时, 17 系统将转入name为root的结果中--> 18 <exception-mapping exception="java.lang.Exception" result="root"/> 19 </global-exception-mappings> 20 21 22 <action name="login" class="zcx.controller.LoginAction"> 23 <!-- 定义局部异常映射, 当Action中遇到MyException异常时, 24 系统将转入name为my的结果中--> 25 <exception-mapping exception="zcx.controller.ZcxException" result="my"></exception-mapping> 26 <result name="my">/WEB-INF/content/exception.jsp</result> 27 <result name="error">/WEB-INF/content/error.jsp</result> 28 </action> 29 30 <!--处理所有的action--> 31 <action name="*"> 32 <!--返回对应的页面--> 33 <result>/WEB-INF/content/{1}.jsp</result> 34 </action> 35 36 37 </package>
时间: 2024-10-11 23:59:32