标签:
index.jsp
1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 <title>测试异常</title> 12 </head> 13 14 <body> 15 <form action="lee/login.action"> 16 用户名:<input type="text" name="username" /><br/> 17 密码:<input type="password" name="password" /><br /> 18 <input type="submit" value="提交" /> 19 </form> 20 </body> 21 </html>
struts.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 8 <constant name="struts.devMode" value="true" /> 9 <package name="lee" extends="struts-default"> 10 <!-- 此处定义所有的全局结果 --> 11 <global-results> 12 <!-- 逻辑名为sql的结果,映射到/sqlexception.jsp页面 --> 13 <result name="sql">/sqlexception.jsp</result> 14 <!-- 逻辑名为root的结果,映射到/exception.jsp --> 15 <result name="root">/exception.jsp</result> 16 </global-results> 17 <!-- 此处定义所有的全局异常映射 --> 18 <global-exception-mappings> 19 <!-- Action抛出SqlException异常时,转入名为sql的结果 --> 20 <exception-mapping result="sql" exception="java.sql.SQLException"></exception-mapping> 21 <exception-mapping result="root" exception="java.lang.Exception"></exception-mapping> 22 </global-exception-mappings> 23 <!-- 下面配置本系统的Action --> 24 <action name="login" class="lee.LoginAction"> 25 <!-- 下面配置了一个局部异常映射,当Action抛出lee.MyException时,转入名为my的结果 --> 26 <exception-mapping result="my" exception="lee.MyException"></exception-mapping> 27 <result name="my">/exception.jsp</result> 28 <result name="error">/error.jsp</result> 29 <result name="success">/welcome.jsp</result> 30 </action> 31 </package> 32 </struts>
MyExction.java
1 package lee; 2 3 public class MyException extends Exception { 4 public MyException(String mess){ 5 super(mess); 6 } 7 }
LoginAction.java
1 package lee; 2 3 import java.sql.SQLException; 4 5 import com.opensymphony.xwork2.Action; 6 7 public class LoginAction implements Action{ 8 private String username; 9 private String password; 10 //封装服务器处理后的结果 11 private String tip; 12 public String getUsername() { 13 return username; 14 } 15 public void setUsername(String username) { 16 this.username = username; 17 } 18 public String getPassword() { 19 return password; 20 } 21 public void setPassword(String password) { 22 this.password = password; 23 } 24 public String getTip() { 25 return tip; 26 } 27 public void setTip(String tip) { 28 this.tip = tip; 29 } 30 31 @Override 32 public String execute() throws Exception { 33 String result=""; 34 if(getUsername().equalsIgnoreCase("user")){ 35 throw new MyException("自定义异常"); 36 } 37 if(getUsername().equalsIgnoreCase("sql")){ 38 throw new SQLException("用户名不能为sql"); 39 } 40 if(getUsername().equals("scott")&&getPassword().equals("tiger")){ 41 setTip("哈哈,服务器提示!"); 42 result=SUCCESS; 43 } 44 else { 45 result=ERROR; 46 } 47 return result; 48 } 49 }
sqlexception.jsp
1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 <%@taglib prefix="s" uri="/struts-tags" %> 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP ‘index.jsp‘ starting page</title> 13 14 </head> 15 16 <body> 17 <h1>sqlexception</h1> 18 <h2><s:property value="exception.message" /> </h2> 19 <h3> 20 <s:property value="exceptionStack" /> 21 </h3> 22 </body> 23 </html>
在登陆页用户名框中输入sql,然后提交
跳转到sqlexception.jsp页面
标签:
原文地址:http://www.cnblogs.com/guoyansi19900907/p/4503282.html