Today, I use Ecipse to build the struts framework and use the struts framework to write a simple example that I believe the reader can easily understand.
Struts is one of the three most popular frameworks in Java today, and the three frameworks are struts,spring,hibernate. Struts framework also includes STRUTS1 and Struts2, due to two different versions of the relatively large, Struts1 has been eliminated, now the enterprise basically all use Struts2, so I certainly also use Struts2.
OK, I'll explain the construction process in detail below.
Start the Eclipse Java EE version (the official recently released version) and create a Web project Struts2
Introducing the jar package in the directory of the Struts2 we downloaded,
The specific directory is as follows
Import these jar packages into the project's Lib directory
Then open the Web. XML configuration file for the upper directory
Copy the contents of filter and filter-mapping two tag pairs to the Web. XML of the STRUTS2 project
Then, want (according to Orrorin tugenensis path to see your struts position) specify the path of the Struts.xml to the project SRC directory
Open the Strut.xml, delete all the contents of the <struts> tag pair or comment, suggest comments
Then copy the content from the comment section into struts
Content
<constant name= "Struts.devmode" value= "false"/>
<package name= "Default" namespace= "/" extends= "Struts-default" >
<default-action-ref name= "index"/>
<action name= "Index" >
<result type= "Redirectaction" >
<param name= "ActionName" >HelloWorld</param>
<param name= "namespace" >/example</param>
</result>
</action>
</package>
Then the first sentence <constant name= "Struts.devmode" value= "false"/>
Change to <constant name= "Struts.devmode" value= "true"/>
The main thing to say is that when the action of our project changes, we can respond to the change immediately without restarting the server.
Result is
To this STRUT2 configuration has been completed, you can write an example of
I'm going to write a simple example here. Print Hello struts2! on a webpage
First create a JSP file in the project's WebContent directory index.jsp
and change the code as follows
Then change the code for Struts.xml in the project to
I'll introduce you to this code later.
OK, start the server, open the browser, enter the URL Http://localhost/Struts2/hello
Result is
Experimental success!!!
Description
As you can see, the address we are visiting now is Http://localhost/Struts2/hello, not the direct access http://localhost/Struts2/index.jsp
I'll draw the process.
When the client accesses, the server filters through the filter and then gets the JSP page accessed by the client based on the action, and then feeds the JSP page back to the client.
What is the benefit of separating the JSP and the client from the invisible? Direct access to JSP is not much easier, why do you make it so complicated?
This design pattern is the same, the advantage behind complex things is that you can expand
Think about it, if you are directly accessing the JSP, if we need to change the contents of the JSP (this is the part that often needs to change), I personally think that in addition to temporarily stop the service, and then change the code, otherwise there is no way! But this is very deadly!
But if we introduce the action of this middle tier, because the client does not have any direct contact with the JSP, so long as the action changes, you can immediately change the JSP, or action unchanged, directly replace the JSP file, you can, no need to pay any price.
This is the core idea of struts, which is very similar to the idea of MVC.
Okay, here's the end of my post. It is believed that the reader can easily understand the idea.
Struts uses javaservlet/jsp technology to implement an application framework for MVC design patterns based on Java eeweb applications