Struts2 Construction of framework



Syria : I learned before hibernate frame ,hibernate Framework is the most object-oriented framework that works on database , What we're learning now is struts2 frame , This framework works on the first layer of the three-tier architecture , To replace the servlet layer , It is better than servlet More advanced , The reason lies in struts2 It encapsulates all kinds of functions , The design pattern is excellent , It's pluggable ( Remove in the filter 、 install 、 Design the interceptor you want to achieve the function division and so on ), And its design idea is also very advanced , It's about thinking in a cross-sectional way (aop); I don't want to talk too much nonsense. From now on ~

1、 Create project

Project name: :struts2


2、 obtain 、 Import jar package

Ready to import shelf package after creation ; Use this jar The package contains a lot of things that are often used in development jar file ~
jar Package file
This jar The bag is old , You can use the updated version of ~
When it comes to importing jar It's packed , Then there must be access jar The path of the package ~

2.1 obtain jar package

It's usually downloaded from the official website on the Internet , The website provided to you :https://struts.apache.org/download.cgi#struts2518, This is to go directly to the select version and download page , The page is shown in the figure below :
 download jar Package web interface
Enter the interface above and select the full version struts2 frame jar Package compression package , Select the file as shown in the figure below :
 Download options


2.2 Import jar package

After downloading, unzip the compressed package and find it apps Folder , There are several packages in this folder , As shown in the figure below :
 Unzip the internal file
And then the first one struts2-blank.war File decompression , Unzip, open and find lib file , You'll see all the jar The document is shown in the figure below :
route : Insert picture description here
struts2-blank.war Internal documents
And then all of the jar Files are copied to the newly created project struts2_1 Of lib Under folder ;( Don't forget it build path Or run it , After running the project eclipse automatically build path, The latter method is recommended ~)
explain :
struts2-blank.war This folder is an example of encapsulation , The others are also examples of encapsulation , however blank This is the most simple and basic , Framework building is suitable for learning how to build and writing a small demo that will do , So use struts2-blank.war This one's inside jar The documents are enough to ;


3、 Import constraints

Constraints, as the name suggests , That is to constrain whether the code we write conforms to the standard , Help us quickly find code writing problems ~

3.1 Import constraints without nets

First , Find the imported jar The bag passed by build path Post production , Its location is in the Java Resources → Libraries Under the document , find struts2-core-2.3.24.jar file , Open and find the constraint file as shown in the figure below :
 Constraint package
We download and use struts2 The development framework file is 2.3 Version of , So choose Import struts-2.3.dtd Version of the constraint file , If you don't have this file, you can make one yourself , It will be below 3.3 How to make a constraint file by yourself ;
Now the default is struts-2.3.dtd The constraint file for , that , We open what we found in the project struts-2.3.dtd This file , Then copy the code snippet as shown in the red box below :
 Constraint import code
Then turn on eclipse Medium Window → Preferences,preferences stay eclipse For parameter configuration in , The panel is shown below :
 Configuration constraints
Enter... In the input box in the upper left corner “catalog” find XML Catalog Select the option and click it to see the following figure :
 Set location
Click... In the big red box Add Button , Import your own... In the first box struts-2.3.dtd Path to file , The second box Key Type Lieutenant general Public ID Change the option to URI, In the third box, enter the path you just copied , The complete picture is shown in the figure below :
 Set the style
And then click OK button ;


3.2 Make a constraint file

If there is no corresponding constraint file, we can make one ourselves , It's easy , Similarly, find the file in the figure below :
 Make constraint reference
Choose what you need ( The default is struts-2.3.dtd) And open , Copy everything in it , Then create a new text document , Glue all the copied code in and save it . Finally, change the name of the document text to be the same as the name of the copied source file , Change the suffix to “.dtd” that will do ;
The reason why you copy the content to the text document and then change the name without directly copying the source file is because the source file dtd Format is not allowed in eclipse Medium direct replication ;


3.3 Import constraints with nets

It can be configured directly when there is a network , The configuration method is as follows :
Open the project libraries Folder , Find one struts-2.3.dtd file , Copy two lines of code , As shown below , The first line is numbered ①, The second line is numbered ②;
 There are net constraints
Then open the window Medium preferences Configure parameters , Find the constraint file configuration (XML Catalog) term , Then number it as ① Paste your code into Key In the box , The number is ② Code to location In the box ,Key type You don't have to change the value of , Still use Public ID, The final configuration is as shown in the figure below :
 Constraint import position


3.4 Test constraints

In the project src Create a struts.xml file ( This is in struts2 There must be in the framework , yes struts2 The core configuration file of the framework ), Then open the struts-2.3.dtd file , Copy the code shown below :
 Constraints added to the code
Paste this code into the struts.xml File header
 Paste position
Then I'll write the code below , Input “<str” There will be completion tips , Mark between the labels after completion “<” It will prompt a bunch of completion prompts, which means that the constraint import is successful ;
 Successfully imported styles

notes :
1) In the case of no network, the use of a network configuration is useless , Network configuration can only be used when there is a network , therefore , It is recommended to use a no network configuration , It's not complicated, but it's easy ;
2) After configuration, if there is no effect, you can try to close it struts.xml Try to open the file again , Make it reload ;


4、Struts2 My first test Demo

4.1 establish Action Method

Create a action class , be known as TestAction:
 establish action
Then create the corresponding method in the class , As shown below ;

package com.java.Action;
public class TestAction {
	public String StrutsTest(){
		System.out.println("You are right!");
		return "success";
	}
}


4.2 stay struts.xml Middle configuration

The main code is as follows , There are three tags in the tag , Don't be 、、 These three ;

<struts>
   <package name="test" namespace="/test" extends="struts-default">
   <action name="TestAction" class="com.java.Action.TestAction" method="StrutsTest">
      <result name="success">/Test.jsp</result>
   </action>
   </package>
</struts>

The label explains :

Tag name Property name Property value remarks
Package name test( arbitrarily ) This is package My name , To distinguish between multiple package, You can write as long as you don't repeat it

namespace /test This is for page Jump , When you write, you have to bring “/“ Talent , But generally only “/” It's OK

extends struts-default Inherit struts The default package for , This is a dead man
Action name TestAction Fill in action Class name , This is a set call called “TestAction” I'm looking for this when I'm in a hurry action Labeled

class com.java.Action.TestAction This action class “TestAction” His whole path , Tell the computer this action Path to class

name success The value of this property is indefinite , According to fill in what to see action The string returned at the end of the class

4.3 Filter configuration

Struts2 It's a filter based framework , therefore , The next step is to configure the filter , The configuration steps are simple , But it's important , Only those with filters struts2 It's real struts2 frame , stay web.xml The code configured in the file is as follows :

<!-- struts2 Core filter configuration  -->
  <filter>
  <filter-name>test</filter-name>
  <!-- struts2 The complete class name that the core filter needs to configure  -->
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>test</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

The difficulty lies in filter The class name of is longer and more difficult to remember , But you don't have to remember , find filter Copy the class name of the class , Find a way to do that : Use CTRL+SHIFT+T The key combination will pop up the search box as shown in the figure below , Enter... In the box “strutspre…” Just enter these letters and you will see an option in the big red box in the figure below , Just click in ;
filter The name of the class to find
Click to enter the category in the big red box , Enter the interface shown in the figure below , Copy the class name in the red box in the figure below and paste it into web.xml In the filter class name of the filter configuration ;
 Copy filter Class name
After configuration, just like the above code ;


4.4 function

everything , Only east wind , Now? All the preparations are ready , It's almost time to run the program , First, right-click on the item Run as → Run on server , The following errors will be reported :
 Error reported
And then after the browser URL, enter before struts.xml Configured in namespace Value , And then the slash and action In the tag name The value of the can ;
 Added data
After adding, the running result is as shown in the figure below :
 Cloud heart results
So it's done struts2 The preliminary construction of the framework ;


《 End of this chapter 》