Next, let's take a look at the basic configuration of struts2. If there are some other configurations that have not been discussed, we will continue.
Struts. xml
1) What is the result if the method attribute is not specified in <action>?
Rename the exeucte method execute2 (), and in struts. xmlDelete method attributesAnd then re-release the project.
HelloWorldAction.java
public class HelloWorldAction extends ActionSupport{
String execute2 () System. out. println ("Welcome to struts2! "}
}
The result is still correct. Why does the method attribute output normally without being written ??
Check ActionSupport and find that the default return value of the execute method is as follows:
A implementation that does nothing an returns "success" method to provide their business logic.
The default value of method is excute (), and the returned value is "success"
2) What is the result if the name attribute is not specified in <result>?
The project is successfully re-released. The default name is "success"
3) What is the result if the type attribute is not specified in <result>?
The project is successfully re-released. We can see that the default type is "dispatcher", that isForwarding by default
If you want to useRedirectionType must be assigned to redirect, that is, type = "redirect", as shown below:
Enter http: // localhost: 8080/struts2/HelloWorldAction in the browser
We found that we have redirected to suc. jsp.
4) Access Action in two ways
> Method 1: Use the. action extension.For example, http: // localhost: 8080/struts2/HelloWorldAction.Action
> Method 2: Do not use the extensionFor example, http: // localhost: 8080/struts2/HelloWorldAction
The two access methods have the same effect.Why?
Line 79th under struts2-core/2.3.16/struts2-core-2.3.16-sources.jar/org/apache/struts2/default. properties has the following settings:
struts.action.extension=action,,
Action or empty
How do I change the default extension?
> Method 1,In the src/main/resource Directory, that is, in the same directory as struts. xml, create struts. properties and write the desired extension.
struts.action.extension=action,hi,amos,,
The. hi and. amos extensions are added here.
The effect is as follows:
> Method 2, Configure the value to struts. xml, as shown below:
Delete struts. properties, redeploy and access the browser:
The second method is recommended.
Note: When struts. properties and struts. xml both exist, struts. properties is used as the primary component.
5) how to configure multiple struts. xml files?
A project is developed by many people, and each person may have their own struts. xml. How can we avoid conflicts and make other people's struts. xml files effective?
As shown in:
Create a directory config and create two configuration files dao_struts.xml and email_struts.xml. How do I load them into the memory? And parsed as a javabean object
UseIncludeYou can specify the file path. Check the startup prompt of tomcat. If the path is incorrect, the system will prompt "Unable to locate...
6) does action need to solve the thread security problem if it uses Singleton or non-singleton mode?
Rewrite the HelloWorldAction constructor as follows:
public HelloWorldAction(){System.out.println("hello actions:"+this.hashCode());}
Multiple accesses in the browser, and then view the console output. The result is as follows:
Note:A request corresponds to an Action instance, so thread security issues are not generated, that is, the synchronized synchronization code block is not generated in the Action.
Source code of this article: click here to view detailed configuration of struts2