关于Struts的注释

Annotation是JDK5.0引入的新特性,以前学JAVA SE时没有感觉到有什么用,但是现在学到Struts2的Action配置时,才算真正体会到Annotation的用处。周所周知,要让URL映射到Action,必须在struts.xml中配置action标签,并制定result。其实这些完全可以在Action类的定义时就解决的,实现了零配置,当然这也是以维护难为代价的。

以下是我的一个实验过程,加深对Annotation的理解:

第一步、搭建环境,这里直接略过

第二步、编写页面(annotation.jsp   index.jsp    failure.jsp)

第三步、编写AnnotationAction

[java]  view plain copy
  1. package cn.guet.hj.action;  
  2.   
  3. import org.apache.struts2.convention.annotation.Namespace;  
  4. import org.apache.struts2.convention.annotation.ParentPackage;  
  5. import org.apache.struts2.convention.annotation.Result;  
  6. import org.apache.struts2.convention.annotation.Results;  
  7.   
  8. import com.opensymphony.xwork2.Action;  
  9. /** 
  10.  *  
  11.  * 使用注释配置Action 
  12.  * 
  13.  */  
  14.   
  15. <span style="color:#CC0000;">@ParentPackage("struts-default")  
  16. @Namespace("/anno")  
  17. @Result(name="success", location="/annotation.jsp")  
  18. @Results({  
  19.     @Result(name="input",location="/index.jsp"),  
  20.     @Result(name="error",location="/failure.jsp")  
  21. })</span>  
  22. public class AnnotationAction implements Action {  
  23.     private String info;  
  24.       
  25.   
  26.     public String getInfo() {  
  27.         return info;  
  28.     }  
  29.   
  30.     public void setInfo(String info) {  
  31.         this.info = info;  
  32.     }  
  33.   
  34.     public String execute() throws Exception {  
  35.         info = "this action is configurate by Annotation!!";  
  36.         return "success";  
  37.     }  
  38.     public String input()throws Exception{  
  39.         return INPUT;  
  40.     }  
  41.     public String error()throws Exception{  
  42.         return ERROR;  
  43.     }  
  44. }  


第四步、配置web.xml,将actionPackages设置为Action的包名

[html]  view plain copy
  1. <filter>  
  2.     <filter-name>struts2</filter-name>  
  3.     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  4.     <init-param>  
  5.         <span style="color:#FF0000;"><param-name>actionPackages</param-name>  
  6.         <param-value>cn.guet.hj.action</param-value></span>  
  7.     </init-param>  
  8.   </filter>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值