Struts2异常处理

异常映射分为两种:局部异常映射,全局异常映射。局部异常映射会覆盖全局异常映射。


JSP请求 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	
	<script type="text/javascript">
		function abc(id){
			var buttonObj = document.getElementById(id);
			buttonObj.style.color="red";
		}
		function efg(id){
			var buttonObj = document.getElementById(id);
			buttonObj.style.color="black";
		}
	</script>
	
  </head>
  
  <body>
    <form action="login.action" method="post">
    	<table align="center">
    		<caption><h2>用户登录</h2></caption>
    		<tr>
    			<td style="font-style: inherit;color: green">用户名:<input type="text" name="username" style="color: red;background: #fffddd" /></td>
    		</tr>
			<tr>
				<td style="font-style: inherit;color: green">密&nbsp;&nbsp;码:<input type="password" name="password" style="color: red;background: #fffddd"/></td>
			</tr>
			<tr align="center">
				<td colspan="2"><input id="1" onmouseover="javascript:abc(this.id)" onmouseout="javascript:efg(this.id)" style="color: black" type="submit" value="登录"/>
				<input type="reset" id="2" onmouseover="javascript:abc(this.id)" onmouseout="javascript:efg(this.id)" style="color: black" value="重填" /></td>
			</tr>
    	</table>
    </form>
  </body>
</html>
 


struts.xml的配置
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
	"http://struts.apache.org/dtds/struts-2.1.dtd">
<!-- struts是Struts 2配置文件的根元素 -->
<struts>
	
	<package name="login" namespace="/" extends="struts-default">
		
		<global-results>
			<result name="sql">/exception.jsp</result>
			<result name="root">/exception.jsp</result>
		</global-results>
		
		<global-exception-mappings >
			<exception-mapping result="sql" exception="java.sql.SQLException" />
			<exception-mapping result="root" exception="java.lang.Exception" />
		</global-exception-mappings>
		
		<action name="login" class="com.lbx.action.LoginAction">
			<exception-mapping result="my" exception="com.lbx.exception.MyException" />
			<result name="my">/exception.jsp</result>
			<result>/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
		
	</package>

</struts>
 
自己定义的一个异常类
package com.lbx.exception;


@SuppressWarnings("serial")
public class MyException extends Exception{

	public MyException() {
	}

	public MyException(String message) {
		super(message);
	}
	
}
 
LoginAction的代码 
package com.lbx.action;

import com.lbx.exception.MyException;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport{
	
	private String username;
	private String password;
	
	//封装处理结果的tip属性
	private String tip;
	public String getTip() {
		return tip;
	}
	public void setTip(String tip) {
		this.tip = tip;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String execute() throws Exception {
		if(this.getUsername().equals("user")){
			throw new MyException("自定义异常");
		}
		if(this.getUsername().equals("sql")){
			throw new java.sql.SQLException("用户名不能为SQL");
		}
		if(this.getUsername().equals("libinxuan")){
			this.setTip("哈哈,服务器提示!");
			return SUCCESS;
		}else{
			this.setTip("登录失败,请重新再登录");
			return ERROR;
		}
	}
	
}
 

exceptin.jsp的代码 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s"  uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>异常处理界面</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">

  </head>
  
  <body>
  	输出异常信息<br>
    <s:property value="exception.message"/><br>
    <s:property value="exceptionStack"/>
    
  </body>
</html>
 


success.jsp和error.jsp没写

 

你可能感兴趣的:(JavaScript,sql,jsp,struts,cache)