Learn the introductory course of struts2 on MOOC.com. The latest struts-2.5.5-all.zip downloaded on the official website. JDK uses jdk1.6 and Tomcat9.0. It keeps reporting errors [java.lang.UnsupportedClassVersionError]. The version is inconsistent, but various versions are not working. Unfortunately, I finally found a newer blog post, which was configured according to his Struts2 version and can finally be used.
-------But I still don't quite understand why struts-2.5.5 is not easy to use whether it is equipped with jdk1.6, jdk1.8, or changing the tomcat version. If you understand clearly, please give me some advice-----
1. Download the Struts2 jar package
Download address: http://archive.apache.org/dist/struts/binaries/
I'm using struts-2.3.14-all.zip
2. Create a webproject project
Below is a project diagram after all files are created.
3. Import the required jar packages for Struts2
Because it is just a sample program, you only need to import Struts2 to support the smallest package. There are different opinions on the added minimum packages online. Here is a method of insurance.
Unzip the compressed package struts-2.3.14-all.zip that you just downloaded. There is a struts2-blank.war package in the apps folder. Open it and go to the WEB-INF/lib directory, as shown in the figure below, which is the minimum package required. The packages included should be related to the specific Struts version.
(Because I used the latest version of struts-2.5.5-all.zip, there is no struts2-blank.war package, which is one of the reasons for abandoning the latest version of struts2)
4. Configure web.xml
The following is the specific configuration coding stage.
What needs to be noted here is:
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
Different versions will be different, and the filter class will be modified according to your own struts version
5. Configure struts.xml
The following needs to create a struts.xml file and configure the action to call strust2. Create it directly in the src directory, so that it will be automatically published to the WEB-INF/classes directory during deployment, or directly create it in the WEB-INF/classes directory.
Among them, the package element functions similar to the Java package mechanism. It is a tool used to classify. The extends attribute is like its name. It inherits all the information of the struts-default package. Generally, it is best to inherit it if we create a package ourselves, because it provides us with most of the functions. You can find this package in the struts-default.xml file in the struts2-core jar package. The action element corresponds to your form, such as your form's action="helloworld". After the form is submitted, the parameters will be handed over to the implementation class of the action name="helloworld" after processing. The result element is the result of an action, which is selected by the control field returned by the action class.
6. Write action class (HelloWorldAction.java)
This class is mainly used for struts2 to jump to this action. The execute() method is executed by default. And return characters according to the result, then struts.xml jumps to the corresponding page according to the returned characters
7. Write a jsp page
Create a new jsp page to present the information. result.jsp
8. Deploy and run
Run the project in Tomcat.
Then open the browser and enter http://localhost:8080/HelloWorldStruts2/helloworld, and you will get the page result corresponding to result.jsp
reference:
Analysis of the process of struts2 and a series of related knowledge codes
Introduction to the beginning of struts2 and code example
Summarize
The above is all the detailed explanation of the introduction to struts2 (building environment, configuration, examples) in this article, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!