`
YiSingQ
  • 浏览: 86959 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Struts2中Action的注解范例

阅读更多
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>
分享到:
评论
2 楼 YiSingQ 2010-06-09  
zzh200411 写道
使用了注解 可以使用<global-exception-mappings> 吗?

可以的。
1 楼 zzh200411 2010-06-08  
使用了注解 可以使用<global-exception-mappings> 吗?

相关推荐

Global site tag (gtag.js) - Google Analytics