您的位置:程序门 -> java -> j2ee / ejb / jms



annotation 问题


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


annotation 问题[已结贴,结贴人:rtdatacn]
发表于:2007-07-31 16:47:04 楼主
@typeconversion(converter   =   "cn.rtdata.yufei.dateconverter ")
@requiredfieldvalidator(message   =   "please   enter   the   date ")
这是我的一个ation中的,谁帮分别解释一下,随便交流一下,annotation!
 完整的代码如下:

import   java.util.date;
import   com.opensymphony.xwork2.actionsupport;
import   com.opensymphony.xwork2.validator.annotations.validation;
import   com.opensymphony.xwork2.validator.annotations.requiredstringvalidator;
import   com.opensymphony.xwork2.validator.annotations.requiredfieldvalidator;
import   com.opensymphony.xwork2.conversion.annotations.conversion;
import   com.opensymphony.xwork2.conversion.annotations.typeconversion;

@validation()
@conversion()

public   class   helloworldaction   extends   actionsupport   {
       
        /**
  *  
  */
private   static   final   long   serialversionuid   =   6497196748231425624l;
private   date   now;
        private   string   name;
       
        @typeconversion(converter   =   "cn.rtdata.yufei.dateconverter ")
        @requiredfieldvalidator(message   =   "please   enter   the   date ")
        public   void   setdatenow(date   now)
     {   this.now   =   now;   }
        public   date   getdatenow()
      {   return   now;   }
     
        @requiredstringvalidator(message   =   "please   enter   a   name ",   trim   =   true)
        public   void   setname(string   name)   {   this.name   =   name;   }
        public   string   getname()   {   return   this.name;   }
       
        public   string   EXECute()   throws   exception   {
                return   success;
        }
}
发表于:2007-07-31 17:04:461楼 得分:16
annotation就是“注解”,是java5引入的新特性。
使用annotation并且结合java的反射机制或者是aop技术,你可以在不影响源程序或者仅仅修改少量代码的情况下,为原来的程序添加新的功能。
例如在你的程序中:
@requiredstringvalidator(message   =   "please   enter   a   name ",   trim   =   true)
public   void   setname(string   name)   {   this.name   =   name;   }
这几句代码。其中的requiredstringvalidator就是一个annotation,根据你的程序,这个annotation的定义可能是:
public   @interface   requiredstringvalidator   {
        public   string   message;
        public   boolean   trim;
};
也就是说,你的setname方法被标注上了一个requiredstringvalidator的注解,并且在这种情况下,message属性被赋予“please   enter   a   name "值,trim属性被设为true。当执行到setname这个方法时,requiredstringvalidator就会起作用。这时你只要通过反射或者aop,就可以捕获到这个注解,并且触发一段新的代码。
这样做的好处是减少程序的耦合,使程序更加面向切面。
发表于:2007-08-01 14:34:382楼 得分:0
在类声明前的,@validation()
@conversion()这两句是什么个义思,是声明吗?那为什么用到的其它三个不声明,还有,像requiredstringvalidator能直接导入的,   是已定义好的吗?像类一样吗?
发表于:2007-08-29 12:35:273楼 得分:2
哥们我也再找这个问题。。帮你顶..加qq:12824338交流一下struts2,顺便说一下我用nb+maven
发表于:2007-08-29 12:40:384楼 得分:2
看看这个地址,也许有帮助:
http://struts.apache.org/2.0.6/docs/validation-annotation.html


快速检索

最新资讯
热门点击