Struts2
Struts2 is developed based on WebWork2. Like struts1, Struts2 also belongs to the MVC framework. But one thing everyone needs to pay attention to is that although the difference in names between Struts2 and struts1 is not very big, the coding styles of Struts2 and struts1 are almost different. So now that we have struts1, why should we launch struts2. Mainly because struts2 has the following advantages:
1>In terms of software design, Struts2 is not as tightly coupled with ServletAPI and strutsAPI as struts1. Struts2 applications do not depend on ServletAPI and strutsAPI. This design of Struts2 is a non-intrusive design, while Struts1 is an intrusive design.
publicclassOrderListActionextendsAction{
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
throwsException{
}
}
2>Struts2 provides an interceptor, which can be used for AOP programming to implement functions such as permission interception.
3>Strut2 provides a type converter, we can convert special request parameters into the required type. In Struts1, if we want to achieve the same function, we must register the type converter with BeanUtil, the underlying implementation of Struts1.
4>Struts2 provides support for a variety of presentation layer technologies, such as: JSP, freeMarker, Velocity, etc.
5>The input verification of Struts2 can verify the specified method, which solves the long-term pain of Struts1.
6>Provides internationalized resource file management implementation in global scope, package scope and Action scope.
Set up Struts2 development environment
1. Find the jar files needed to develop Struts2 applications
The minimum jar files required to develop Struts2
struts-core-2.xxjar: Struts2 framework core class library
xwork-2.xxjar: XWork class library, Struts2 is built on top of it
ognl-2.6.jar: For image navigation language (ObjectGraphNavigationLanguage), the Struts2 framework reads and writes object properties through it
freemarker-2.3.x.jar: Struts2 UI label template is written using freeMarker
commons-logging-1.1.x.jar: A log package produced by ASF. The Struts2 framework uses this log package to support Log4J and JDK1.4+ logging.
commons-filteupload-1.2.1.jar file upload component, the file must be added after version 2.1.6
2. Write the Struts2 configuration file
The default configuration file of Struts2 is struts.xml. This file needs to be stored under WEB-INF/classes. The configuration template of this file is as follows:
<?xmlversion="1.0"encoding="utf-8"?>
<!DOCTYPEstrutsPUBLIC".//ApatcheSoftwareFoundation//DTDStrutsConfiguration2.0//EN""http://struts.apatche.org/dtds/struts-2.0.dtd">
<struts>
</struts>
3. Add Struts2MVC framework startup configuration to Web.xml
In struts1.x, the struts framework is started through Servlet. In struts2, the struts framework is started through Filter. His configuration in web.xml is as follows:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apatche.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!--Since Struts2.1.3, the following FilterDispatcher has been marked as obsolete
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>-->
</filter>
<filter=mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*<url-pattern>
</filter-mapping>
In the init() method of StrutsPerpareAndExecuteFilter, the default configuration file struts.xml in the class path will be read to complete the initialization operation.
Introduction to packages in Struts.xml configuration
<packagename="itcast"namespace="/test"extends="struts-defult">
<actionname="helloworld"class="cn.itcast.action.HelloWorldAction"method="execute">
<resultname="success">/WEB-INF/page/hello.jsp</result>
</action>
</package>
To find the jar file it needs to develop, you need to decompress it: struts-2.3.8-all.gz
Or struts-2.3.8-apps.gz finds the files under struts-2.3.8-all/struts-2.3.8/apps as follows:
A blank project struts2-blank is provided, and we can develop struts2 on this project.
What should I do if I need the support of other jar files during the development process?
You need to find the corresponding jar file in /struts-2.3.8-all/struts-2.3.8/lib.
How to decompress the struts2-blank.war project - copy it to webapps in tomcat and start tomcat
Find startup.bat in apache-tomcat/bin and click to start
observe:
You can open the browser IE
Enter http://localhost:8080/struts2-blank
Find the decompressed jar file: apache-tomcat/webapps/struts2-blank/WEB-INF/lib
Bug records:
1. java.net.BindException:Addressalreadyinuse:JVM_Bind
Analysis: A bug is caused by the tomcat you are using not being shut down before being started for the second or n times.
Solution:
Just close the started tomcat.
HTTPStatus404-/demo/servlet/demo.do
Analysis: The URL you requested cannot be found: servlet/demo.do
Solution:
Check if your URL exists? If it does not exist, add it according to the business.
3.
ThereisnoActionmappendforactionnamedemo.
Analysis: The corresponding demo action cannot be found on the server side
Demo.action
/csdn/demo.action
Only prompt: demo not found
demoaction not found under /csdn
Bug
Noresultdefinedforactioncom.opensymphony.xwork2.ActionSupportandresultsuccess