[Java Web] Struts2 add up (one)

Source: Internet
Author: User

Struts2 Environment Configuration
    1. Import STRUTS2 need jar package
    2. Create a Struts.xml file under the web-inf/classes (SRC) folder
    3. Adding struts filter information to the Web. xml file
frequently use configuration files
    1. Struts.xml: Used to store the action mapping relationship, set up struts configuration information
    2. Web. XML: Struts filter for configuring containers, general configuration such as the following:
      <filter>      <filter-name>struts2</filter-name>      <filter-class>          Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter      </filter-class>  </ filter>  <filter-mapping>      <filter-name>struts2</filter-name>      <url-pattern >/*</url-pattern>  </filter-mapping>
    3. Struts-default.xml:struts2 default configuration information, generally not recommended changes
    4. The default settings information for the default.properties:Struts2. Same not recommended changes
Action
The core functionality of STRUTS2 is typically made up of two parts: the action map of Struts.xml and the action entity class written in Java. In Struts.xml, you typically use a series of tags, such as <action>, <result>, <package>, to complete the action mapping, with their properties:
  1. Package

    must

    descriptive narrative

    / td>

    name

    Yes

    extends

    namespace

    no

    abstact

    no


  2. Action

    Property

    Whether you must

    Descriptive narrative

    Name

    Is

    of the request Action name

    Class

    Whether

    Action handle the corresponding detailed path of the class

    Method

    Whether

    Specify Action the method name in

    Converter

    Whether

    Specify Action types of converters used


  3. Result

    Property

    Whether you must

    Descriptive narrative

    Name

    Whether

    Action returns the view name. I feel success

    Type

    Whether

    Return the result type, default feel dispatcher

A simple action map can be written as:
<package name= "admin" extends= "Struts-default" ><action name= "Welcome" class= "Struts.action.WelcomeAction" ><result>/welcome.jsp</result></action></package>

The action entity class needs to inherit the Actionsupport class and rewrite his Execute method (or any other method that returns a string type and corresponds to the method property in the action tag). and returns the string corresponding to the result label Name property. The entity classes corresponding to the above configuration are:
public class Welcomeaction extends Actionsupport {public String execute () {return SUCCESS;}}

Dynamic Method Invocation (DMI)
The ability to configure only one method in an action is inconvenient in practical applications, and STRUTS2 supports specifying different methods of invoking the action class in the URL. The format is "action name! Method name".

Before using this feature, you need to include the following code in Struts.xml to open DMI:

<constant name= "Struts.enable.DynamicMethodInvocation" value= "true"/>
It is then easy to invoke the corresponding method in the action class with the return value of type string.


wildcard character (wildcard)
STRUTS2 supports wildcard operations. This reduces the effort by contract format when a large number of actions need to be configured. It uses "*" instead of the easy-to-transform word, and then takes the {n} out. n represents the nth wildcard character. For example, a program needs to have 4 action. The names are dogaction, Cataction, Birdaction, and Fishaction, respectively.

Assume that the previous method requires the configuration of the 4 action mapping relationship. However, using wildcards only requires one of the following action:

<action name= "buy*" class= "struts.action. {1} Action "><result>/pay.jsp</result></action>
The wildcard has a wide scope, and the action's name can change the result of method, class, and result, and so on. Often use can save a lot of time. However, a good consistency lattice formula is required beforehand.


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

[Java Web] Struts2 add up (one)

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.