Usage of Struts2 II

Several Development Modes for Action

1. Write a class that inherits ActionSupport and can use some of its functions
2. Write a class to implement the Action interface, override the execute method, and do not use the method defined in ActionSupport
3. Neither inherits nor implements. Write a method directly in this class to handle our logic, and nothing can be done.

Wildcards for struts

<action name="user_*_*" class="" method={1}">
<result name="{1}">/{2}.jsp</result>
</action>

Path matching for struts

Namespace="/" default namespace
Namespace="/a" custom namespace
localhost:8080/project name/a access path
Matching principle
If it is a path such as localhost:8080/project name/a/b/c, then it can be found as follows
*/a/b/c
*/a/b
*/a

Constant use of struts
Find default.properties file
Modify Constants Inside

 <!--This is the maximum size of the uploaded file represented-->
     <constant name="struts.multipart.maxSize" value="123456789"></constant>
     <!--Set is struts Coding for-->
     <constant name="struts.i18n.encoding" value="UTF-8"></constant>
     <!--Set is struts Default Access Suffix
       value="action The suffix must be action Otherwise, it will not be accessible"
       value="action,do":Representation suffix can only be action perhaps do Can't be anything else
       value="action,do,":Representation suffixes can be action perhaps do Or no suffix
     -->
     <constant name="struts.action.extension" value="action,do,"></constant>
     <!--To allow dynamic methods to be invoked, dynamic methods are actionName Add directly on the basis of!Adding a method name so that it directly specifies the execution of a method is called a dynamic method      -->
     <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
     <!--xml Automatically load files after changes-->
     <constant name="struts.configuration.xml.reload" value="true"></constant>

Default configuration for struts
Global result set (principle local priority)

<global-results>
             <result name="success">/index.jsp</result>
     </global-results>

Because struts-default has a default action configuration

 <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />

Actions can omit classes and methods
Data processing for struts
1. Complete data placement into domain objects via the API of the Servlet
(Problems create struts-servlet coupling problems)

         HttpServletRequest request = ServletActionContext.getRequest();
         HttpSession session = request.getSession();
         ServletContext application = ServletActionContext.getServletContext();
         //Add data to a domain object
         request.setAttribute("request_","Here is request Object Data");
         session.setAttribute("session_","Here is session Object Data");
         application.setAttribute("application_","Here is application Object Data");

2 Get domain objects through ActionContext

         ActionContext context = ActionContext.getContext();
         //Getting Domain Objects from Instances
         Map<String, Object> session = context.getSession();
         Map<String, Object> application = context.getApplication();
         Map<String, Object> request = context.getContextMap();
         //Add data to a domain object
   session.put("session_", "Session Data");
   request.put("request_", "resuest Data");
   application.put("application_", "application Data");

3. Putting data into domain objects by implementing RequestAware or SessionAware or ApplicationAware
* and set method for three fields * struts automatically injects us into the domain object
This method can be used as a base class

 public class CustomAction extends ActionSupport implements RequestAware,SessionAware,ApplicationAware
{
private Map<String, Object> application;
private Map<String, Object> session;
Map<String, Object> request;
}

Data auto-encapsulation for struts

1. Create javabean objects
2. Create a class to inherit ActionSupport
Write in this class
Method 1. set Method Injection

Note how previous versions were injected:
Using the construction method (in eclipse)

private User user=new User();

Current version of the injection method (MyEclipse):

private User use
public void setUser(User user) {
        this.user = user;
}
public User getUser() {
        return user;
}

Converter for date type
Write a class to inherit StrutsTypeCover
Re-method

convertFromString: Converts a string to another type
convertToString: Convert other types to strings

Write a configuration file

1. The profile name must be ActionName-conversion.properties.
Format java.util.Date=action full path
2. Content of the configuration file: properties of the converter JavaBean object. Properties
3. Configuration files must be located in the same directory as Action

Keywords: Struts Session JSP xml

Added by dirkdetken on Tue, 09 Jul 2019 20:14:46 +0300