This article was last updated on <2017 December 06 > Struts2 version number: 2.5.14.1
What's NEW: Version number of struts2 and related lib, example can be run correctly
Introduction
This article focuses on how to use struts2 in Eclipse, the version of STRUTS2 that the article uses is 2.5.14.1,
There will be a little bit of difference with the other versions, as explained in the article. Example of the full source at the end of the text, the pro-test without any errors.
struts2 Download
Website Download address USTC Mirror Station download address
The latest version is 2.5.14.1, this version of some jar package and the old version is not the same, but the change is not big.
Specific changes suggested read the 2.5 version of Version-notes
Figure 1 struts2 download page
Choose the full distribution download here.
Download the extracted file structure as shown below:
Figure 2 STRUTS2 File structure diagram
Apps are examples of using STRUTS2, Docs is documentation, including Help documentation and API documentation, LIB is a jar package, SRC is source code.
a simple example
Eclipe
Environment
Required environment Tomcat I'm using: tomcat-8.0.42 JDK > 7 I'm using:jdk-1.8.0_131< Eclipse I'm using: Eclipse Neon (4.6.2)
The first thing to do is to ensure that Tomcat is working properly, and the configuration of Tomcat is not spoken here.
Detailed Steps
Create a new Web project named HelloWorld
Build a common Web project in eclipse. Ensure that the project is operational.
to add a related jar package
To copy the required jar packages from the struts2 Lib directory to the Web-inf/lib folder, the most basic need is 8 jar packages:
Commons-fileupload-1.3.3.jar, Commons-io-2.5.jar, Commons-lang3-3.6.jar, Freemarker-2.3.26.jar,
Log4j-api-2.9.1.jar, Ognl-3.1.15.jar, Struts2-core-2.5.14.1.jar, Javassist-3.20.0-ga.jar
Note: The previous version of struts2.5 is a bit different and requires Xwork-core.jar, Log4j-api-2.7.jar is not required. The reason is struts2.5 put xwork source
Merged into the Struts-core. The logging API was used before struts2.5, and struts2.5 was replaced with the Log4j 2 API.
If you follow my advice and read struts2.5 's version-notes, you'll know the details.
Figure 3 Required Jar Package
Configure the core controller of the STRUTS2 framework in Web. XML Strutsprepareandexexutefilter
Figure 4 Configuration of Web. xml
The full class name of filter struts2.5 was previously org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.
More changes please read struts2.5 version-notes
Create a new business control action class in the SRC directory, inherited from Com.opensymphony.xwork2.ActionSupport, with the following content:
Figure 5 Creating a new action
action needs to be configured in the core configuration file of Struts2
STRUTS2 's core configuration file is Struts.xml, which is placed in the SRC directory.
Figure 6 Struts.xml Content
Notice where the struts.xml is placed.
Create a new result.jsp file for action to display the returned view
Figure 7 The contents of result.jsp
Last Run HelloWorld project, in browser access Http://localhost:8080/HelloWorld/helloworld
The final presentation should be the content of the result.jsp.
Console output Action's print content
Here, struts2 even if the configuration is complete.
the code involved
Xml
<?xml version= "1.0" encoding= "UTF-8"?> <web-app
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "Xmlns=" Http://xmlns.jcp.org/xml/ns/javaee "xsi:schemalocation=" Http://xmlns.jcp.org/xml/ns/javaee http// Xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "id=" webapp_id "version=" 3.1 ">
<display-name>helloworld </display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--Configuring the core Interceptor
--<filter> <!--filter name
-- <filter-name>struts2</filter-name>
<!--filter Implementation class struts2.5 may have been different before--
< Filter-class>org.apache.struts2.dispatcher.filter.strutsprepareandexecutefilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<!-- Block all URLs-
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app >
Struts.xml
<?xml version= "1.0" encoding= "UTF-8"?> <!
DOCTYPE struts public
"-//apache software foundation//dtd struts Configuration 2.5//en"
"/http Struts.apache.org/dtds/struts-2.5.dtd ">
<struts>
<package name=" Default "namespace="/" extends= "Struts-default" >
<!--name of the action, accessed using Helloworld.action Access, class: implementation---
<action Name= "HelloWorld" class= "cn.xhcoding.action.HelloWorldAction" >
<!--result set, which is the view returned by success in action--
<result>
/result.jsp
</result>
</action>
</package>
</ Struts>
Helloworldaction.java
Package cn.xhcoding.action;
public class Helloworldaction extends com.opensymphony.xwork2.actionsupport{
@Override public
String Execute () throws Exception {
System.out.println ("executing action");
Returns the view SUCCESS, which is the framework definition for
return SUCCESS;}
}
result.jsp
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "
pageencoding= "UTF-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>action result</title>
<body>
</body>
This article permanently updates the address
Reference URL:
STRUTS2 Official Document: http://struts.apache.org/docs/
struts2.5 version changed: http://struts.apache.org/docs/version-notes-25.html