Struts2 Environment Configuration
- Import STRUTS2 need jar package
- Create a Struts.xml file under the web-inf/classes (SRC) folder
- Adding struts filter information to the Web. xml file
frequently use configuration files
- Struts.xml: Used to store the action mapping relationship, set up struts configuration information
- 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>
- Struts-default.xml:struts2 default configuration information, generally not recommended changes
- 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:
- Package
|
must |
descriptive narrative / td> |
name |
Yes |
|
extends |
|
|
namespace |
no |
|
abstact |
no |
|
- 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 |
- 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)