Configure action using Annotation

Source: Internet
Author: User

This is a struts2.0.11 application. For usage instructions, see the comments section of the Code.

The first is the Web. xml file.

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/javaee <br/> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <br/> <filter-Name> struts2 </filter-Name> <br/> <Filter -Class> Org. apache. struts2.dispatcher. filterdispatcher </filter-class> <br/> <init-param> <br /> <! -- The actionpackage is in a fixed format. Then specify the package of the action that uses the annotation --> <br/> <param-Name> actionpackages </param-Name> <br/> <param-value> COM. jadyer. action </param-value> <br/> </init-param> <br/> </filter> <br/> <filter-mapping> <br/> <Filter -Name> struts2 </filter-Name> <br/> <URL-pattern>/* </url-pattern> <br/> </filter-mapping> <br/> <welcome-file-List> <br/> <welcome-File> annotationtest. JSP </welcome-File> <br/> </welcome-file-List> <br/> </Web-app>

Then, enter the user name and password to add the user page annotationtest. jsp

<% @ Page Language = "Java" pageencoding = "UTF-8" %> </P> <p> <H2> Configure action with struts2 annotation </H2> </P> <p> <p> <font color = "red"> note: </font> the user name and password set by the program are <font color = "blue"> <strong> admin </strong> </font> and <font color = "blue"> <strong> jadyer </strong> </font> </p> <br/> <p> <font color = "red"> note: </font> when the user name or password is incorrect, it will remain on this page. </p> </P> <p> <form action = "<% = request. getcontextpath () %>/annotation/user. action "method =" Post "> <br/> name: <input type =" text "name =" username "> <br/> password: <input type = "text" name = "password"> <br/> <input type = "Submit" value = "Add User"> <br/> </form>

The usersuc. jsp page showing the user's added results

<% @ Page Language = "Java" pageencoding = "UTF-8" %> <br/> <% @ taglib prefix = "S" uri = "/Struts-tags" %> <br/> <H2> Add User <font color = "blue"> <s: property value = "username"/> </font> Successful </H2>

Useraction. Java

Package COM. jadyer. action; </P> <p> Import Org. apache. struts2.config. namespace; <br/> Import Org. apache. struts2.config. result; <br/> Import Org. apache. struts2.config. results; </P> <p> Import COM. opensymphony. xwork2.actionsupport; </P> <p>/******************** [Zero Configuration ]******** **************************************** *******************************/<br/> // in the official documents, zero configuration is called Zero Configuration, translation to China is called annotation configuration. The advantage is that you do not need to write struts. and write the configuration information in action. <br/> // spring also has two configuration methods, another method is to use annotations to configure <br/> // so if you force yourself to understand the annotations here, you will learn about spring in the future, it can be simpler <br/> // and the annotation will become more and more widely used in the future, because it has many advantages. The annotation function in struts2.0.11 is not yet very powerful <br/> // according to the official website, after struts2.2 is released, the annotation function will be very powerful, you can leave struts. XML is used to write programs, which makes it easier for programmers to develop <br/> // everyone can configure their own programs without focusing on struts. XML is configured, but uniformity will be missing. In case someone does not follow the routine, difficult to maintain <br/>/********************* [analysis of the four annotations of action ]**** **************************************** ***********************************/<br // action-related annotations in all struts2 Annotations: parentpackage, namespace, result, and results <br/> // At Org. apache. strut The s2.config package contains four classes of the same name, which are related to the four annotations <br/> // these four annotations are class-level, that is, they need to be written before the class, next, I will explain it one by one <br/> // [@ parentpackage (value = "struts-Default ")] is equivalent to [<package extends = "struts-Default"/>] <br/> // and the default setting of the parentpackage annotation inherits the Struts-default package, therefore, it can be left empty <br/> // [@ namespace (value = "")] is equivalent to [<package namespace = ""/>] or a namespace is not defined in the package <br/> // The default setting of the namespace annotation is located in the default namespace, therefore, it can also be left empty <br/> // [@ result (name = "success", value = "/usersuc. JSP ")] is equivalent Name = "success">/usersuc. JSP </result>] <br/> // The default value of name attribute of result annotation is success. However, to facilitate analysis, we recommend that you explicitly specify the name attribute <br/> // After configuring the annotation in the action. which classes are declared using annotations in XML, the Code is as follows <br/> // <filter> <br/> // <filter-Name> struts2 </filter-Name> <br/> // <filter-class> Org. apache. struts2.dispatcher. filterdispatcher </filter-class> <br/> // <init-param> <br/> // <! -- The actionpackage is in a fixed format. Then specify the package of the action that uses the annotation --> <br/> // <param-Name> actionpackages </param-Name> <br/> // <param-Value> COM. jadyer. action </param-value> <br/> // </init-param> <br/> // </filter> <br/> // when the action is named must end with action, during annotation interpretation, the action following the name is removed first <br/> // then the field before the action name is used as the access name, for example, the call of useraction, for example, <br/> // access [<a href = "user. action "mce_href =" user. action "> Add User </a>], and the first letter of the user must be in lowercase to match successfully <br/> // If action returns multiple results, for testing, you need to use 【@ Results: Annotation. For more information, see <br/> // Note: If type = "Redirect" is set when the forwarding mode of result is configured, Type mismatch is reported: cannot convert from string to class error <br/> // note: this is because inheritance is allowed in annotations, therefore, you need to write the Redirect class in the type value <br/> // Note: for example, type = org. apache. struts2.dispatcher. servletredirectresult. class <br/> // Note: Then strtus2 will automatically load this class using the reflection mechanism. You cannot add double quotation marks because they represent the complete class, not a string <br/> /********************************* **************************************** **************************************** * ******/</P> <p> // @ parentpackage (value = "struts-Default ") <br/> // @ namespace (value = "") <br/> // @ result (value = "/usersuc. JSP ") <br/> // @ result (name =" success ", value ="/usersuc. JSP ") <br/> @ namespace (value ="/annotation ") <br/> @ results ({<br/> @ result (name =" success ", Value = "/usersuc. JSP "), <br/> @ result (name =" input ", value ="/annotationtest. JSP ", type = org. apache. struts2.dispatcher. servletredirectresult. class) <br/>}) <br/> public class useraction extends actionsupport {<br/> Private Static final long serialversionuid =-5137568969521746637l; </P> <p> private string username; <br/> private string password; </P> <p>/* the setter and getter corresponding to the two attributes */</P> <p> @ override <br/> Public String execute () throws exception {<br/> system. out. println ("useraction.exe cute () is invoked"); <br/> If (username. trim (). equalsignorecase ("admin") & password. equals ("jadyer") {<br/> return success; <br/>}else {<br/> return input; <br/>}< br/> // @ before <br/> // public void dobefore () {<br/> // system. out. println ("the annotation configuration method is called before execute () Execution "); <br/> //} <br/> // @ After <br/> // public voi D doafter () {<br/> // system. out. println ("the annotation configuration method is called After Execute () Execution "); <br/> //} <br/> // @ beforeresult <br/> // public void dobeforeresult () {<br/> // system. out. println ("the annotation configuration method is called before the result is returned "); <br/> //} <br/>/********************** [three annotations of interceptor ]* **************************************** ********************************/<br/>/ /In COM. opensymphony. xwork2.interceptor. the annotations package defines the annotations for available configuration interceptors <B R/> // the before and after annotations are called Before and After Execute, the beforeresult annotation is called before the result set is returned. <br/> // The Four annotations of action are class-level, while the interceptor annotations are method-level, that is to say, they all need to be written before the methods in the class <br/> // if you want to use annotations in the interceptor, you must configure xwork2.interceptor. the annotationworkflowinterceptor class of the annotations package <br/> // The annotationworkflowinterceptor class is the prerequisite for calling these three annotations. You must configure this class in struts. in XML, the configuration is as follows <br/> // <interceptors> <br/> // <interceptor name = "annointerceptor" class = "com. opensymphony. Xwork2.interceptor. annotations. annotationworkflowinterceptor "/> <br/> // </interceptors> <br/> // Add <Interceptor-ref name = "annointerceptor"/> You can <br/> // note, at this time, do not forget to reference the default defaultstack interceptor <br/> // when sending this action request, the execution is: @ before ---- execute () ---- @ beforeresult ---- @ After sequence <br/> // in this case, it is very easy to configure the interceptor using annotations. However, annotations also have one disadvantage: they cannot be reused <br/> // In addition, we can also implement permission verification by configuring the interceptor through annotations. Example: <br/> // @ before <br/> // Public String dobefore () {<br/> // If (Session. get ("user") = NULL) {<br/> /// if no session exists, it indicates that the user has not logged on, then return the logon result set <br/> // return login; <br/> //} else {<br/> /// if a session exists, it indicates that the user has logged on and can proceed to the next step <br/> /// null is returned here, then the program will continue to execute () method <br/> // return NULL; <br/> //} <br/> // This allows you to easily verify the user's access permissions. Therefore, use annotations to configure interceptor, sometimes it is very convenient <br/> /****************************** **************************************** **************************************** **********/

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.