STRUTS2 annotation-based action configuration

Source: Internet
Author: User

The biggest benefit of using annotations to configure action is that you can implement zero configuration, but transactions are both pros and cons, easy to use, and less convenient to maintain.

To use annotations, we must add an additional package: Struts2-convention-plugin-2.x.x.jar.

Although the 0 configuration, but Struts.xml is still indispensable, the configuration is as follows:

<?xml version= "1.0" encoding= "UTF-8"?>

<! DOCTYPE Struts Public

"-//apache software foundation//dtd Struts Configuration 2.1.7//en"

"Http://struts.apache.org/dtds/struts-2.1.7.dtd" >

<struts>

<!--How to encode the request parameters--

<constant name= "struts.i18n.encoding" value= "UTF-8"/>

<!--Specifies the type of request suffix that is processed by STRUTS2. Multiple separated by commas-

<constant name= "struts.action.extension" value= "action,do,htm"/>

<!--whether to reload when Struts.xml is changed. The default value is False (used in a production environment), and the development phase is best opened-

<constant name= "Struts.configuration.xml.reload" value= "true"/>

<!--whether to use Struts's development model. The development pattern will have more debugging information. The default value is False (used in a production environment), and the development phase is best opened-

<constant name= "Struts.devmode" value= "false"/>

<!--set whether the browser caches static content. The default value is True (used in a production environment), and the development phase is best off--

<constant name= "Struts.serve.static.browserCache" value= "false"/>

<!--Specifies that spring is responsible for the creation of the Action object

<constant name= "struts.objectfactory" value= "Spring"/>

-

<!--whether to turn on dynamic method invocation--

<constant name= "Struts.enable.DynamicMethodInvocation" value= "false"/>

</struts>

Note for the action class:

Package com.tjcyjd.web.action;

Import org.apache.struts2.convention.annotation.Action;

Import org.apache.struts2.convention.annotation.ExceptionMapping;

Import org.apache.struts2.convention.annotation.ExceptionMappings;

Import Org.apache.struts2.convention.annotation.Namespace;

Import Org.apache.struts2.convention.annotation.ParentPackage;

Import Org.apache.struts2.convention.annotation.Result;

Import Org.apache.struts2.convention.annotation.Results;

Import Com.opensymphony.xwork2.ActionSupport;

/**

* STRUTS2 Annotation-based action configuration

*

*/

@ParentPackage ("Struts-default")

@Namespace ("/annotation_test")

@Results ({@Result (name = "Success", location = "/main.jsp"),

@Result (name = "Error", location = "/error.jsp")})

@ExceptionMappings ({@ExceptionMapping (exception = "Java.lange.RuntimeException", result = "error")})

public class Loginaction extends Actionsupport {

Private static final long serialversionuid = 2730268055700929183L;

Private String LoginName;

private String password;

@Action ("Login")//or write @Action (value = "Login")

Public String Login () throws Exception {

if ("Yjd". Equals (LoginName) && "Yjd". Equals (password)) {

return SUCCESS;

} else {

return ERROR;

}

}

@Action (value = "add", results = {@Result (name = "Success", location = "/index.jsp")})

Public String Add () throws Exception {

return SUCCESS;

}

Public String Getloginname () {

return loginName;

}

public void Setloginname (String loginName) {

This.loginname = LoginName;

}

Public String GetPassword () {

return password;

}

public void SetPassword (String password) {

this.password= password;

}

}

This completes a note-based action configuration.

Summarize the commonly used annotations as follows:

Namespace: Specifies the namespace.

Parentpackage: Specifies the parent package.

Result: Provides a mapping of the action result. (Mapping of a result)

Results: "Result" annotation list

Resultpath: Specifies the base path of the resulting page.

Action: Specifies the access URL for the action.

Actions: List of "Action" annotations.

Exceptionmapping: Specifies the exception mapping. (Mapping a Declaration exception)

Exceptionmappings: An array of first-level declaration exceptions.

Interceptorref: Interceptor Reference.

Interceptorrefs: Interceptor Reference Group.

STRUTS2 annotation-based action configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.