Struts2 knowledge Review 4

Source: Internet
Author: User
Tags html form
Overview of struts2 interceptor 1 requirements
In the CRM system, the user logon function is available. If the visitor knows the access page path in the background, he or she can directly access the CRM system without logon. Therefore, he or she must intercept the user who has not logged in.
2 interceptor
(1) overview * It is used in AOP (Aspect-Oriented progaramming) to intercept operations before accessing a method and field. * Interception is an implementation policy of AOP. * In the Chinese Document of struts2 webwork, the interceptor is an object that dynamically intercepts action calls. (2) role of the interceptor in strust2 * interceptor before executing action * blocking and processing operation * blocking execution before executing action * vertical repetition of Action Code horizontal extraction (3) interceptor chain) it is called an interceptor stack in struts2 ). The interceptor chain forms a chain for the interceptor in a certain order, and the interceptor in the interceptor chain is called one by one in order. (4) Most of the interceptor's implementation principles. The interceptor methods are called by proxy. The implementation process of the struts2 interceptor is: when the request reaches the servletdispatcher of struts2, based on the interceptor configuration in the strut2.xml configuration file, struts2 instances are converted into an interceptor object, serialized into a list, and finally called the interceptor in the list one by one. The Interceptor is an implementation of AOP, Which is pluggable. The struts2 interceptor stack links the interceptor in order and is called in order. (5) custom interceptor class (Life Cycle of interceptor: created with project startup and destroyed with project closure) <1> Method 1: Implement interface interceptor, you need to implement the corresponding method public class myinterceptor implements interceptor {// destroy @ override public void destroy () {system. out. println ("myinterceptor destruction");} // create @ override public void Init () {system. out. println ("myinterceptor initialization");} // Interception Method @ override Public String intercept (actioninvocation arg0) throws exception {system. out. println ("myinterceptor Interception Process Code ..... "); return NULL ;}}< 2> Method 2: inherit an abstract class abstractinterceptor * its internal source code helps us implement the intercepter interface and implement inint () and destroy () method * makes it possible to use the intercept () public class myinterceptor2 extends abstractinterceptor {@ override Public String intercept (actioninvocation arg0) throws exception {system without implementing these two methods. out. println ("interceptor2 interception processing"); return NULL ;}< 3> method 3: Inherit the main functions of the methofilterinterceptor class: Customize the interception methods of the Interceptor-custom Method to intercept and does not need to intercept public class myinterceptor3 extends methodfilterinterceptor {@ override protected string dointercept (actioninvocation Invocation) throws exception {// pre-processing code system of the filter. out. println ("myinterceptor3 pre-processing code");/* allow -- If you execute this allow method, it means that all the interceptors must be recursive and then * will be returned to the result in the action, if you do not execute this method, the result is directly returned to the action (for result processing) */invocation. invoke (); // The post-processing code of the filter system. out. println ("post-processing code of myinterceptor3 "); Return NULL ;}}( 6) configure the filter <1> example Step 1: Custom Operations <interceptors> <! -- Custom interceptor --> <interceptor name = "myinterceptor" class = ""> </interceptor> <! -- Create a custom interceptor stack and put the custom interceptor into the stack --> <Interceptor-stack name = "myinterceptorstack"> <Interceptor-ref name = "myinterceptor"> <Param name = "includemethods"> </param> <Param name = "excludemethods"> </param> </Interceptor-ref> <! -- Reference the 20 interceptors of the default interceptor Stack --> <Interceptor-ref name = "defaultstack"> </Interceptor-ref> </Interceptor-stack> </interceptors> Step 2: is struts. in XML, <package> specify the custom interceptor stack as the default interceptor stack <default-interceptor-ref name = "mystack"> </default-interceptor-ref> Step 3: specify the custom interceptor stack for Action <action> <Interceptor-ref name = "mystack"> </Interceptor-ref> </Action> <2> example <! -- Custom interceptor configuration --> <interceptors> <! -- 1 register the interceptor --> <interceptor name = "myinterceptor3" class = "cnxxxa. Interceptor. myinterceptor3"> </interceptor> <! -- 2 register the interceptor stack and put the custom interceptor into the stack --> <Interceptor-stack name = "mystack"> <! -- We recommend that you import the interceptor stack of the custom interceptor before the default 20 interceptor stack because if the custom interceptor has an error message, later interceptors will help with the processing --> <Interceptor-ref name = "myinterceptor3"> <! -- Specifies which methods do not intercept --> <! -- <Param name = "excludemethods"> add, delete </param> --> <! -- Specifies the methods to intercept, and the two can only be configured with one --> <Param name = "includemethods"> add, delete </param> </Interceptor-ref> <! -- Reference the default 20 interceptor stacks --> <Interceptor-ref name = "defaultstack"> </Interceptor-ref> </Interceptor-stack> </interceptors> <! -- 3 specify the default interceptor stack in the package as the custom interceptor Stack --> <default-interceptor-ref name = "mystack"> </default-interceptor-ref> <! -- 4 specify the Interceptor (stack) for the action. This interceptor only acts on 21 interceptors in the action at the moment. (1 custom + 20 default) --> <action name = "demo1action _ *" class = "CN. ixxx. interceptor. demo1action "method =" {1} "> <Interceptor-ref name =" mystack "> </Interceptor-ref> <result name =" success ">/Show. JSP </result> </Action>
Struts2 label Library 1 Overview
<1> in the early days, JSP development embedded a large number of java scripts, which was not conducive to maintenance and reading. The struts2 tag library language came into being. <2> strust2 tag library is defined in a file for struts2-core-2.3.24.jar/META-INF/struts-tags.tld. <3> to use the struts2 tag library, you must use the taglib command in JSP to import: <% @ taglib uri = "/Struts-tags" prefix = "S" %>
2 struts2 tag Library category (common tag and UI tag)

(1) Common tags include control tags and data tags)
<1> Control labels are used to control conditional logic, cyclic logic, and set operations.

  • Conditional logic <s: If test = ""> </S: If> <s: elseif test = ""> </S: elseif> <s: else> </S: else>
    Example:
    <S: If test = "# list. Size () = 2">
    The list length is 2.
    </S: If>
    <S: elseif test = "# list. Size () = 1">
    The list length is 1.
    </S: elseif>
    <S: else>
    The length of lsit is different.
    </S: else>
  • Loop logic <s: iterator> </S: iterator>
    Example 1: Simple set element Iteration Example 2: iteration after adding the step attribute Aaaa Example 3: add an iteration of the Status attribute Number of elements currently traversed:
    The last element of the Set:
    The first element of the Set:
    ========================================================== ====
    Current index value:
    The element index is odd:
    The element index is an even number. <2> data tag (used to output background data and complete other data access .)
  • <S: Property> used to output the specified value
    Attributes of the tag:
    • ID (optional) ID of the specified Element
    • If the default (optional) output value is null, the default property value is displayed.
    • Escape (optional) Specifies whether to ignore HTML code
    • Value (optional) specifies the output attribute value. If no value is specified, the value at the top of the valuestack stack is output by default.
      Example:
      1. If the value attribute is not written, the top value of the output value stack is:
        <S: iterator value = "# list">
        <S: property/>
        </S: iterator>
      2. Default property, enter the default value: <s: property value = "" default = "here is the default value of Flower"/>
      3. Ignore HTML code: <s: property value = "'
      4. Do not ignore the HTML code: <s: property value = "'
  • <S: A> used to construct a hyperlink to an HTML page, similar to <A>
    Has the following attributes:
    • Id specifies its ID
    • Href specifies the hyperlink address
    • Action specifies the jump action name
    • Namespace: namespace for jump action
    • Method specifies the method in the jump action.
      Example:
      <S: a href = "xxx.com"> xxx.com </s: A>
      <S: A action = "floweraction" namespace = "/" method = "list"> </s: A>
  • <S: Debug> used for debugging. It mainly inputs information in valuestack and stackcontext.
  • <S: Include>
  • <S: param>

    (2) UI tags include form tags, non-form tags, and Ajax tags.
    <1> form tags are used to generate HTML form elements.

    • Struts2 form tag benefits
      Benefit 1: a set of built-in styles
      Benefit 2: it automatically displays the tag value based on the value in the stack, and does not need value = "$ {user. name} as before}
    • Theme attribute: Specifies the topic of the form.
      XHTML: Default
      Simple: no subject
    • Example:
      <S: Form Action = "demotag2action" namespace = "/" theme = "XHTML">
      <S: textfield name = "name" label = "username"> </S: textfield>
      <S: password name = "password" label = "password"> </S: Password>
      <S: Radio list = "{'male', 'female '}" name = "gender" label = "gender"> </S: Radio>
      <S: Radio list = "# {1: 'male', 0: Female}" name = "gendar" label = "gender"> </S: Radio>
      <S: Radio list = "# {0: 'playing basketball ', 1: 'swimming', 2: 'soccer play'} "name =" habits "label =" hobbies "> </S: Radio>
      <S: select list = "# {0: '', 1: '', 2: 'master'} "headerkey =" "headervalue =" -- select -- "name =" edu "label =" education "> </S: Select>
      <S: file name = "photo" label = "Recent"> </S: File>
      <S: textarea name = "DESC" label = "resume"> </S: textarea>
      <S: Submit value = "Submit"> </S: Submit>
      </S: Form> <2> non-form tags are used to generate HTML tags and output information encapsulated in actions. <3> Ajax tags provide Ajax technical support.
Struts. xml file to configure global exception result set processing.
Example: <global-exception-mappings> <! -- Result = "error" is used to associate error results in action with exception = "associated with the corresponding exception class --> <exception-mapping result =" error "exception =" Java. lang. runtimeexception "> </exception-mapping> </Global-exception-mappings> <action name =" "class =" "method =" "> <result name =" error "> xxx. JSP </result> </Action> // error message <s: property value = "exception. message "/>

Struts2 knowledge Review 4

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.