action配置 异常处理 使用注释配置action

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<!-- default 在默认的命名空间 -->
<package name="default" extends="struts-default">
<!-- 全局结果定义 -->
<global-results>
 <result name="login" type="redirect">/login.action</result>
 <result name="sqlException" type="chain">/sqlException.action</result>
 <result name="exception">/exception.jsp</result>
</global-results>
<global-exception-mappings>
<!-- 全局异常映射定义 -->
<exception-mapping exception="java.sql.SQLException" result="sqlException"/>
<exception-mapping result="exception" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
	<action name="login" class="...">
	<!-- 局部异常定义 -->
	  <exception-mapping exception="rog.coolove.MyException" result="login"/>
	  <result type="redirectAction">${nextAction}</result>
	</action>
</package>

 在页面中可以<s:property value="exception.message"/>

<s:property value="exceptionStack"/>

访问异常信息

 

 

注释配置acton

import org.apache.struts2.config.Namespace;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.apache.struts2.dispatcher.ServletDispatcherResult;

import com.opensymphony.xwork2.Action;
@ParentPackage("default")
@Namespace("/test")
@Result("/success.jsp")
@Results({
    @Result(name="input", value="/input.jsp"),
    @Result(name="error", value="/error.jsp", type=ServletDispatcherResult.class)
})
public class AnnotationAction implements Action
{
  @Override
  public String execute() throws Exception
  {
    return SUCCESS;
  }

  public String input() throws Exception
  {
    return INPUT;
  }

  public String error() throws Exception
  {
    return ERROR;
  } 
}
  
 <PRE class=xml name="code"><?xml version="1.0" encoding="UTF-8"?>   
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   
    <welcome-file-list>   
        <welcome-file>index.jsp</welcome-file>   
    </welcome-file-list>   
  
    <filter>   
        <filter-name>struts2</filter-name>   
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>   
        <init-param></PRE>   
<PRE class=xml name="code"><!-- 需要配置filter-->   
            <param-name>actionPackages</param-name>   
            <param-value>org.coolove.action</param-value>   
        </init-param>   
    </filter>   
  
    <filter-mapping>   
        <filter-name>struts2</filter-name>   
        <url-pattern>/*</url-pattern>   
    </filter-mapping>   
</web-app></PRE>   
   
 

你可能感兴趣的:(apache,jsp,xml,Web,struts)