strus2注解

本文深入探讨了Struts2注解的使用方法及其好处,包括注解的导入、配置、作用和应用实例。介绍了Struts2注解的几个关键组件及其功能,如解析注解的插件、常量配置、Action注解等,并通过HelloWorld程序展示了注解的实际应用。

使用struts2的注解,注解的好处:类被移动一般不用修改代码或配置文件

学习的时候,一般先从xml下手,因为更贴近原理性的东西;但是工作的时候,往往使用注解,因为简单。应该在学习完xml后,深入理解struts2各种组件后,再学习注解就非常简单了

1.struts22.3.7的版本lib中导入的包:

asm-3.3.jar

asm-commons-3.3.jar

commons-fileupload-1.2.2.jar

commons-io-2.0.1.jar

commons-lang3-3.1.jar

commons-logging-1.1.1.jar

freemarker-2.3.19.jar

javassist-3.11.0.GA.jar

ognl-3.0.5.jar

struts2-config-browser-plugin-2.3.7.jar

struts2-convention-plugin-2.3.7.jar

struts2-core-2.3.7.jar

struts2-dojo-plugin-2.3.7.jar

xwork-core-2.3.7.jar

其中主要是struts2-convention-plugin-2.3.7.jar起到解析注解的作用

这里的struts2-config-browser-plugin-2.3.7.jar是开发时用来看映射关系的,导入这个包后在地址栏输入http://localhost:8080/应用名字/config-browser/actionNames.action就可以查看工程中有哪些action每个action的详细映射信息,以及项目中的常量信息等

2.虽然使用注解不过还是要有配置文件struts.xml,里面置空即可,也可以设置一些常量(在xml中和struts.properties中均可)

struts2-convention-plugin-2.3.7.jar中提供了几个常量:

struts.convention.default.parent.package:这个常量表示缺省的包名是什么,相当于struts.xml中的extends属性。这个常量可以配也可以不配;不配默认是convention-default,在struts2-convention-plugin-2.3.7.jar中struts-plugin.xml定义了这个常量,还可以看到:<packagename="convention-default" extends="struts-default"></package>convention-default就是struts-default

struts.convention.package.locators:这个常量表示action类的所在包的包名的后缀是什么:比如action。这个常量也可以不配;不配置的默认值:action,actions,struts,struts2,那也就是说,如果我们的Action类的包名不是以这些结尾的,那么就会扫描不到

struts.convention.package.locators.basePackage: 这个常量表示你的action类的所在包的名字是什么,表示在这个包以及子包(符合struts.convention.package.locators定义的结尾)下面查找所有的Action。这个常量也可以不配;不配置默认为"",值只能是一个包,不能写多个包

struts.convention.action.suffix:<constantname="struts.convention.action.suffix" value="Action"/>定义了表示以Action为后缀的都会被解析为struts的Action,当然这个Action要在符合条件(struts.convention.package.locators.basePackag,struts.convention.package.locators)包下面,而且如果这个Action没有实现Action接口,里面必须有@Action注解才会被解析成Action,如果不是以这个指定的字符串结尾,那么必须实现Action接口才会被解析成Action

 

总之: Action要被解析为Action必须满足以下条件:

必须条件: Action类在struts.convention.package.locators.basePackage包或者子包中

必须条件: struts.convention.package.locators=action,actions,struts,struts2  Action所在包名的结尾

必须条件: (自己写的Action类实现了Action接口)或者(自己写的Action类以struts.convention.action.suffix指定的字符串结尾并在类中添加了@Action注解)

 

3.写HelloWorld应用程序,参考

http://www.2cto.com/kf/201110/106826.html

 

4.对注解的介绍:

@ParentPackage

作用在类上或包上,只有一个value字段

This annotation canbe used directly on Action classes or in thepackage-info.java class inorder to specify the default XWork parent package for all actions in the Javapackage. The search order for XWork parent packages is therefore:

1.  Any ParentPackage annotations placed on individual action classes

2.  Any ParentPackage annotations placed in the package-info.java file

3.  The struts configuration property struts.convention.default.parent.package

 

@Namespace

@Target({ElementType.PACKAGE, ElementType.TYPE})

只有一个属性value

The value of a Namespace annotation shouldspecify the portion of the action URL between the context path and the actionname.

Thisannotation can also be placed inside the special Java file namedpackage-info.java,which allows package level annotations. If this is used in this manner itchanges the default namespace for all actions within that Java package. Thesearch order for the namespace of a particular class is therefore:

  1. If the Action     annotation exists within the action and specifies a full URI (i.e. it     starts with a / character)
  2. Any     Namespace annotations placed on individual action classes
  3. Any     Namespace annotations placed in the package-info.java file
  4. The namespace as determined using the Java package name and the standardconvention based naming

 

 

 


@Action

@Target({ElementType.METHOD, ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

public@interfaceAction {

    String DEFAULT_VALUE ="DEFAULT_VALUE";

    /**

     * Allows actions to specify different URLsrather than the default that is based on the package

     * and action name. This also allowsmethods other than execute() to be invoked or multiple URLs

     * to map to a single class or a singlemethod to handle multiple URLs.

     *

     @return The action URL.

     */

    String value() defaultDEFAULT_VALUE;

    /**

     * Allows action methods to specificallycontrol the results for specific return values. These

     * results are not used for othermethod/URL invocations on the action. These are only used for

     * the URL that this action is associatedwith.

     *

     @return The results for the action.

     */

    Result[] results() default {};

 

    /**

     * Allows action methods to specify whatinterceptors must be applied to it.

     @return Interceptors to beapplied to the action

     */

    InterceptorRef[] interceptorRefs()default {};

 

    /**

     @return The parameters passed to the action. This isa list of strings that form a name/value

     *         pair chain since creating a Map for annotations is not possible. Anexample would be:

     *         <code>{"key", "value", "key2","value2"}</code>.

     */

    String[] params() default {};

 

    /**

     @return Maps return codes toexceptions. The "exceptions" interceptor must be applied to theaction.

     */

    ExceptionMapping[] exceptionMappings()default {};

 

    /**

     * Allows actions to specify differentclass name.

     *

     @return The class name forthe action.

     */

    String className() defaultDEFAULT_VALUE;

}

Thisannotation can be used to control the URL that maps to a specific method in anAction class. By default, the method that is invoked is the execute method ofthe action and the URL is based on the package and class names. This annotationallows developers to change the URL or invoke a different method. This alsoallows developers to specify multiple URLs that will be handled by a singleclass or a single method.

Thiscan also be used via the Actions annotationto associate multiple URLs with a single method.

 

 

 

@Results

代码:

@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.TYPE})

public@interfaceResults {

    Result[] value();

}

This annotation allows a class to define morethan oneResultannotations

 

 

 

@result

代码:

@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.TYPE})

public@interfaceResult {

    /**

     @return The name of the result mapping. This is thevalue that is returned from the action

     *         method and is used to associate a location with a return value.

     */

    String name() defaultcom.opensymphony.xwork2.Action.SUCCESS;

 

    /**

     @return The location of the result within the webapplication or anywhere on disk. This location

     *         can be relative if the type of this result is one of the pre-defined relativeresult

     *         types (these default to dispatcher, velocity and freemarker). Or thislocation can be

     *         absolute relative to the root of the web application or the classpath(since velocity

     *         and freemarker templates can be loaded via the classpath).

     */

    String location() default"";

 

    /**

     @return The type of the result. This is usually setupin the struts.xml or struts-plugin.xml

     *         and is a simple name that is mapped to a result Class.

     */

    String type() default"";

 

    /**

     @return The parameters passed to the result. This isa list of strings that form a name/value

     *         pair chain since creating a Map for annotations is not possible. Anexample would be:

     *         <code>{"key", "value", "key2","value2"}</code>.

     */

    String[] params() default {};

}

Thisannotation is used to specify non-convention based results for the Strutsconvention handling. This annotation is added to a class and can be used tospecify the result location for a specific result code from an action method.Furthermore, this can also be used to handle results only for specific actionmethods within an action class (if there are multiple).

Whenthis annotation is used on an action class, it generates results that areapplicable to all of the actions URLs defined in the class. These areconsidered global results for that class. Here is an example of a globalresult:

  @Result(name="fail", location="failed.jsp")
 public class MyAction {
 }
 

Thisannotation can also be used inside an Action annotationon specific methods. This usage will define results for that specific actionURL. Here is an example of an action URL specific result:

  @Action(results={@Result(name="success", location="/", type="redirect")})}
 public String execute() {
 }
 

 

 

 

 

@InterceptorRef

代码:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

public@interface InterceptorRef {

    /**

     @return name of theinterceptor or interceptor stack

     */

    String value();

 

    /**

     @return The parameters passed to the interceptor.This is a list of strings that form a name/value

     *         pair chain, since creating a Map for annotations is not possible. Anexample would be:

     *         <code>{"key", "value", "key2","value2"}</code>.

     */

    String[] params() default {};

}

This annotationallows an interceptor to be applied to an action. If this annotation is used atthe class level, then the interceptor will be applied to all actions defined onthat class, and will be applied before the ones defined at the method level.

 

 

 

 


@InterceptorRefs

代码:

@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.TYPE})

public@interfaceInterceptorRefs {

    InterceptorRef[] value();

}

 

This annotation allows a class to define morethan oneInterceptorRefannotations.

 

 

 

 


@ExceptionMappings

代码:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

public@interface ExceptionMappings {

    /**

     @return Exception mappings

     */

    ExceptionMapping[] value();

}

Thisannotation allows a class to define more than oneExceptionMappingannotations. These exception mappings will be on all actions defined in theannotated class, they are not global exception mappings.

 

 

 

@ ExceptionMapping

代码:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

public@interfaceExceptionMapping {

    /**

     @return Result name

     */

    String result();

 

    /**

     @return Class name of the exception to be thrown

     */

    String exception();

 

    /**

     @return The parameters passed to the exception. Thisis a list of strings that form a name/value

     *         pair chain since creating a Map for annotations is not possible. Anexample would be:

     *         <code>{"key", "value", "key2","value2"}</code>.

     */

    String[] params() default {};

}

 

Adds an exceptionmapping to an action

 

验证,只需在方法上面写@ Validations,例如:

    @Validations(

          requiredFields={

                 @RequiredFieldValidator(fieldName="age",message="age isnull"),

                 @RequiredFieldValidator(fieldName="name",message="name isnull"),

                 }

          ,

          requiredStrings={

                 @RequiredStringValidator(fieldName="name",message="${getText('age.required.invalide')}")

                 },

          regexFields={

                 @RegexFieldValidator(fieldName="name",message="名字格式错误",expression="[a-z]{3,20}")

                 }

    )

    public String add(){

       return"success";

    }

 

 

 

@ SkipValidation

代码:

/**

 * Marks an Action method to not be validated

 */

@Retention(RetentionPolicy.RUNTIME)

@Target(METHOD)

public@interfaceSkipValidation {

}

表示某方法跳过验证

 

@Target( { ElementType.METHOD, ElementType.TYPE })

@Retention(RetentionPolicy.RUNTIME)

public@interfaceValidations {

 

    /**

     * Custom Validation rules.

     */

    publicCustomValidator[]customValidators()default {};

 

    publicConversionErrorFieldValidator[] conversionErrorFields()default {};

 

    publicDateRangeFieldValidator[] dateRangeFields()default {};

 

    publicEmailValidator[] emails()default {};

 

    publicFieldExpressionValidator[] fieldExpressions()default {};

 

    publicIntRangeFieldValidator[] intRangeFields()default {};

 

    publicRequiredFieldValidator[] requiredFields()default {};

 

    publicRequiredStringValidator[] requiredStrings()default {};

 

    publicStringLengthFieldValidator[] stringLengthFields()default {};

 

    publicUrlValidator[] urls()default {};

   

    publicConditionalVisitorFieldValidator[] conditionalVisitorFields()default {};

 

    publicVisitorFieldValidator[] visitorFields()default {};

 

    publicRegexFieldValidator[] regexFields()default {};

 

    publicExpressionValidator[] expressions()default {};

}

 

If you want to useseveral annotations of the same type, these annotations must be nested withinthe @Validations() annotation.

Annotation usage:

Used at METHOD level.

Annotationparameters:

Parameter

Required

Notes

requiredFields

no

Add list of RequiredFieldValidators

customValidators

no

Add list of CustomValidators

conversionErrorFields

no

Add list of  ConversionErrorFieldValidators

dateRangeFields

no

Add list of DateRangeFieldValidators

emails

no

Add list of EmailValidators

fieldExpressions

no

Add list of FieldExpressionValidators

intRangeFields

no

Add list of IntRangeFieldValidators

requiredStrings

no

Add list of RequiredStringValidators

stringLengthFields

no

Add list of StringLengthFieldValidators

urls

no

Add list of UrlValidators

visitorFields

no

Add list of VisitorFieldValidators

regexFields

no

Add list of RegexFieldValidator

expressions

no

Add list of ExpressionValidator

Example code:

 

@Validations(

requiredFields = {@RequiredFieldValidator(type = ValidatorType.SIMPLE, fieldName ="customfield", message ="You mustenter a value for field.") },

requiredStrings = {@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName ="stringisrequired", message ="You must enter avalue for string.") },

emails = {@EmailValidator(type =ValidatorType.SIMPLE, fieldName ="emailaddress", message ="You mustenter a value for email.") },

urls = { @UrlValidator(type =ValidatorType.SIMPLE, fieldName ="hreflocation", message ="You mustenter a value for email.") },

stringLengthFields = {@StringLengthFieldValidator(type = ValidatorType.SIMPLE, trim =true, minLength ="10", maxLength ="12", fieldName ="needstringlength", message ="You mustenter a stringlength.") },

intRangeFields = {@IntRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName ="intfield", min ="6", max = "10", message = "bar mustbe between ${min} and ${max}, current value is ${bar}.") },

 dateRangeFields = {@DateRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName ="datefield", min ="-1", max = "99", message = "bar mustbe between ${min} and ${max}, current value is ${bar}.") },

expressions = {

          @ExpressionValidator(expression ="foo >1", message ="Foo must be greater than Bar 1. Foo = ${foo}, Bar = ${bar}."),

          @ExpressionValidator(expression ="foo >2", message ="Foo must be greater than Bar 2. Foo = ${foo}, Bar = ${bar}."),

          @ExpressionValidator(expression ="foo >3", message ="Foo must be greater than Bar 3. Foo = ${foo}, Bar = ${bar}."),

          @ExpressionValidator(expression ="foo >4", message ="Foo must be greater than Bar 4. Foo = ${foo}, Bar = ${bar}."),

          @ExpressionValidator(expression ="foo >5", message ="Foo must be greater than Bar 5. Foo = ${foo}, Bar = ${bar}.") })

    public String execute()throws Exception {

       returnSUCCESS;

      }

标题SpringBoot智能在线预约挂号系统研究AI更换标题第1章引言介绍智能在线预约挂号系统的研究背景、意义、国内外研究现状及论文创新点。1.1研究背景与意义阐述智能在线预约挂号系统对提升医疗服务效率的重要性。1.2国内外研究现状分析国内外智能在线预约挂号系统的研究与应用情况。1.3研究方法及创新点概述本文采用的技术路线、研究方法及主要创新点。第2章相关理论总结智能在线预约挂号系统相关理论,包括系统架构、开发技术等。2.1系统架构设计理论介绍系统架构设计的基本原则和常用方法。2.2SpringBoot开发框架理论阐述SpringBoot框架的特点、优势及其在系统开发中的应用。2.3数据库设计与管理理论介绍数据库设计原则、数据模型及数据库管理系统。2.4网络安全与数据保护理论讨论网络安全威胁、数据保护技术及其在系统中的应用。第3章SpringBoot智能在线预约挂号系统设计详细介绍系统的设计方案,包括功能模块划分、数据库设计等。3.1系统功能模块设计划分系统功能模块,如用户管理、挂号管理、医生排班等。3.2数据库设计与实现设计数据库表结构,确定字段类型、主键及外键关系。3.3用户界面设计设计用户友好的界面,提升用户体验。3.4系统安全设计阐述系统安全策略,包括用户认证、数据加密等。第4章系统实现与测试介绍系统的实现过程,包括编码、测试及优化等。4.1系统编码实现采用SpringBoot框架进行系统编码实现。4.2系统测试方法介绍系统测试的方法、步骤及测试用例设计。4.3系统性能测试与分析对系统进行性能测试,分析测试结果并提出优化建议。4.4系统优化与改进根据测试结果对系统进行优化和改进,提升系统性能。第5章研究结果呈现系统实现后的效果,包括功能实现、性能提升等。5.1系统功能实现效果展示系统各功能模块的实现效果,如挂号成功界面等。5.2系统性能提升效果对比优化前后的系统性能
在金融行业中,对信用风险的判断是核心环节之一,其结果对机构的信贷政策和风险控制策略有直接影响。本文将围绕如何借助机器学习方法,尤其是Sklearn工具包,建立用于判断信用状况的预测系统。文中将涵盖逻辑回归、支持向量机等常见方法,并通过实际操作流程进行说明。 一、机器学习基本概念 机器学习属于人工智能的子领域,其基本理念是通过数据自动学习规律,而非依赖人工设定规则。在信贷分析中,该技术可用于挖掘历史数据中的潜在规律,进而对未来的信用表现进行预测。 二、Sklearn工具包概述 Sklearn(Scikit-learn)是Python语言中广泛使用的机器学习模块,提供多种数据处理和建模功能。它简化了数据清洗、特征提取、模型构建、验证与优化等流程,是数据科学项目中的常用工具。 三、逻辑回归模型 逻辑回归是一种常用于分类任务的线性模型,特别适用于二类问题。在信用评估中,该模型可用于判断借款人是否可能违约。其通过逻辑函数将输出映射为0到1之间的概率值,从而表示违约的可能性。 四、支持向量机模型 支持向量机是一种用于监督学习的算法,适用于数据维度高、样本量小的情况。在信用分析中,该方法能够通过寻找最佳分割面,区分违约与非违约客户。通过选用不同核函数,可应对复杂的非线性关系,提升预测精度。 五、数据预处理步骤 在建模前,需对原始数据进行清理与转换,包括处理缺失值、识别异常点、标准化数值、筛选有效特征等。对于信用评分,常见的输入变量包括收入水平、负债比例、信用历史记录、职业稳定性等。预处理有助于减少噪声干扰,增强模型的适应性。 六、模型构建与验证 借助Sklearn,可以将数据集划分为训练集和测试集,并通过交叉验证调整参数以提升模型性能。常用评估指标包括准确率、召回率、F1值以及AUC-ROC曲线。在处理不平衡数据时,更应关注模型的召回率与特异性。 七、集成学习方法 为提升模型预测能力,可采用集成策略,如结合多个模型的预测结果。这有助于降低单一模型的偏差与方差,增强整体预测的稳定性与准确性。 综上,基于机器学习的信用评估系统可通过Sklearn中的多种算法,结合合理的数据处理与模型优化,实现对借款人信用状况的精准判断。在实际应用中,需持续调整模型以适应市场变化,保障预测结果的长期有效性。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值