1. org. apache. struts2.dispatcher. ng. filter. StrutsPrepareAndExecuteFilter preparation and execution
2,
<filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
The url-pattern Convention is used to write only/*, and there is no need to write *. action
3,
<package name="default" namespace="/" extends="struts-default"> <action name= "hello"> <result> /Hello.jsp </result> </action> <span style="white-space:pre"></span></package>
Namespace = "/" corresponds to the access path;
/Hello. action
The action is accessed and the result is returned;
The default action can be omitted;
4. Associate structs source code with java docs
Right-click the jar file and choose properies> Java Source Attachment.
Set Source Code
D:/Program Files/struts-2.1.6/src/core/src/main/java
Javadoc documentation
File:/D:/Program Files/struts-2.1.6/docs/struts2-core/apidocs/
Set xml prompt:
A) window-preferences-search catalog-add
B) Select the key type as URI.
C) key: http://struts.apache.org/dtds/struts-2.0.dtd
D) location: The corresponding dtd file, located in the struts-core package, extract open, specify the corresponding location, such as D:/Program Files/struts-2.1.6 \ lib \ struts2-core-2.1.6 \ struts-2.0.dtd
5. structs Operating Mechanism
Client-> url-> Http request-> Tomacat-> Find the corresponding Webapplication-> web. xml-> filter doFilter method->
<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>
-> Refer to struct. xml-> Find the corresponding namespace-> Find the corresponding action-> Find the corresponding result-> feedback result-> request Forwad to the target file
<package name="default" namespace="/" extends="struts-default"><action name="index"><result>/index.jsp</result></action> </package>
6. namespace determines the action access path. The default value is "", which can receive actions in all paths.
Namespace can be written as/,/xxx, or/xxx/yyy,
The corresponding action access path is/index. action,/xxx/index. action, or/xxx/yyy/index. action.
Namespace should also be named by modules.
<constant name="struts.devMode" value="true" /> <package name="front" extends="struts-default" namespace="/front"> <action name="index"> <result>/Namespace.jsp</result> </action> </package> <package name="main" extends="struts-default" namespace=""> <action name="index"> <result>/Namespace.jsp</result> </action> </package>
Struct. xml Analysis
Package: prevents the duplicate and conflict of actions;
Namespace = "/front", must begin with a slash, namespace should also be named by the module;
If no namespace is specified, it is equivalent to namespace = "", meaning that all actions are assigned
Namespace processing;
Process: first find the action in the corresponding path for matching. If not, find the action with the namespace being empty,
If no action is found, an error is returned;
7. The returned view can be determined by the user-defined Action;
The specific method is to locate the corresponding configuration item based on the returned string to determine the View content;
<constant name="struts.devMode" value="true" /><package name="front" extends="struts-default" namespace="/"><action name="index" class="com.struts2.front.action.Index"><result name="success">/ActionIntroduction.jsp</result></action> </package>
The implementation of specific actions can be a common java class with the public String execute method.
Action interface;
package com.bjsxt.struts2.front.action; import com.opensymphony.xwork2.Action; public class IndexAction1 implements Action {@Overridepublic String execute() {return "success";} }
Configuration Analysis:
<Class = "com. bjsxt. struts2.front. action. Index">
Struct. xml-> Find the corresponding class-> instantiate object-> execute the corresponding execute () method
Execution Process:
Read xml-> action is class-> Find the class Object (a new object is required for each access)->
When no class is configured, the default class is ActionSupport.
ActionSupport source code
public String execute() throws Exception { return SUCCESS;}
The most common method is to inherit from ActionSupport. The advantage is that the Struts2 encapsulated method can be used directly.
package com.struts2.front.action; import com.opensymphony.xwork2.ActionSupport; public class IndexAction2 extends ActionSupport {@Overridepublic String execute() {return "success";} }
Cause: ActionSupport has encapsulated many methods that can be called directly for us and can be used directly in subclass.
The above content is based on the tutorial by Jack Ma.