欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Struts2中Action的注解范例 博客分类: 开源技术 StrutsApacheJSPXMLWeb 

程序员文章站 2024-02-16 21:52:28
...
struts配置文件
<?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>
</struts>  


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;   
  }    
}  


web.xml文件
<?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>   
	<!-- 需要配置filter-->
            <param-name>actionPackages</param-name>      
            <param-value>cn.com.sise.expert.web</param-value>      
        </init-param>      
    </filter>      
     
    <filter-mapping>
        <filter-name>struts2</filter-name>      
        <url-pattern>/*</url-pattern>      
    </filter-mapping>      
</web-app>