Struts2的聲明式異常源碼分析

配置文件:繼承了struts-default包,默認的攔截器是struts-default.xml文件中的defaultStack

vo:

package com.example.user.vo;

public class User {
	private String username;
	private String password;
	
	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;
	}
}	
action:
package com.example.user.action;


import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	   /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
		

	@Override
       public String execute() {
	      
			@SuppressWarnings("unused")
			int i = 10/0;
		
	      return "success";
	   }  
	   
}

<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>


	<package name="default" extends="struts-default" namespace="/">
		<action name="userAction" class="com.example.user.action.UserAction" >
		<exception-mapping result="index" exception="java.lang.ArithmeticException"></exception-mapping>
			
			<result name="index">/index.jsp</result>
			<result name="success">/success.jsp</result>
		</action>
	</package>
</struts>

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ 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>My JSP 'index.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  	
  <body>
  		<s:debug></s:debug>
  		<s:property value="exception"/>
  		<br><br>
  		${exception}
  		<br><br>
       <s:form action="userAction">
       	<s:textfield name="username" label="username"></s:textfield>
       	<s:textfield name="password" label="password"></s:textfield>
       	<s:submit/>
       </s:form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'success.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    success page
  </body>
</html>

圖1.

圖2.

圖3.

圖4.

圖5.


點擊Debug:圖6.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章