Introduction to struts 2 (build environment, configuration and examples)
Learn the introduction course of struts 2 on the website, and download the latest struts-2.5 on the official website 5-all. Zip, JDK uses jdk1 6,Tomcat9. 0. The error [Java. Lang. unsupported classversionerror] has been reported. The versions are inconsistent, but it is difficult to change the versions. However, finally, I found a newer blog post, configured according to his struts 2 version, and finally it can be used.
------But I still don't quite understand why struts-2.5 5 whether it is equipped with jdk1 6 or jdk1 8. It's still difficult to replace the Tomcat version. If you have a clear understanding, please give me more advice------
1. Download the jar package of struts 2
Download address: http://archive.apache.org/dist/struts/binaries/
I use struts-2.3 14-all. Zip this version
2. Create a web project project
The following shows the engineering diagram after all files are created.
3. Import the jar package required by struts 2
Because it is only an example program, you only need to import the minimum package supported by struts 2. There are different opinions on the added minimum package on the Internet. Here is an insurance method.
Unzip the compressed package struts-2.3 14-all. Zip, there is a struts 2-blank.zip in the apps folder War package, open it and go to the WEB-INF / lib directory, as shown in the figure below, which is the minimum package required. The package included should be related to the specific struts version.
(because the latest version of struts-2.5.5-all.zip used before has no struts-2-blank.war package, which is one of the reasons for abandoning the latest version of struts 2)
4. Configure web xml
Next, go to the specific configuration coding stage.
Note here:
org. apache. struts2. dispatcher. ng. filter. StrutsPrepareAndExecuteFilter
The filter class will be different with different versions, and it will be modified according to its own struts version
5. Configure struts xml
Next, you need to create struts XML file to configure the action to be called by strust2. Directly create a new one in the SRC directory, so that it will be automatically published to the WEB-INF / classes directory or directly created under the WEB-INF / classes directory during deployment.
Among them, the function of package element is similar to the mechanism of java package. It is a tool for classification. The extensions attribute is the same as its name. It inherits all the information of struts default package. Generally, it is best to inherit it when we create a package ourselves, because it provides us with most functions, You can use struts - default in the jar package of struts 2 - core The package was found in the XML file. The action element corresponds to your form, for example, your form's action = "HelloWorld". After the form is submitted, the parameters will be handed over to the implementation class of action's name = "HelloWorld" for processing. The result element is the result of the action, which is selected by the control field returned by the action class.
6. Write action class (helloworldaction. Java)
This class is mainly used after struts 2 jumps to this action. The execute () method is executed by default. And return characters according to the result, and then struts XML jumps to the corresponding page according to the returned characters
7. Write JSP page
Create a new JSP page to render the information. result. jsp
8. Deployment run
Run the project in Tomcat
Then open the browser and enter http://localhost:8080/HelloWorldStruts2/helloworld , you will get result JSP corresponding page results
referance:
Struts 2 process and a series of related knowledge code analysis
Introduction to struts 2 and code examples