Struts 2 requires Servlet API 2.4 or higher, JSP 2.0 or higher, and Java 5 or higher.
Add Struts 2 Jar Files To Class Path
Find and copy to WEB-INF\lib these files (note X.X.X.X.jar refers to the version number):
- asm-x.x.jar
- asm-commons-x.x.jar
- asm-tree-x.x.jar
- commons-io-X.X.X.jar
- commons-lang3-X.X.X.jar + commons-lang3-3.1.jar
- commons-fileupload-X.X.X.jar
- freemarker-X.X.X.jar
- javassist-X.X.X.jar
- ognl-X.X.X.jar
- struts2-core-X.X.X.X.jar
- xwork-core-X.X.X.jar
Add Struts 2 Servlet Filter
To enable the Struts 2 framework to work with your web application you need to add a Servlet filter class and filter mapping to web.xml. Below is the filter and filter-mapping nodes you should add to web.xml.
web.xml Servlet Filter
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Create struts.xml
Struts 2 can use either an XML configuration file or annotations (or both) to specify the relationship between a URL, a Java class, and a view page (such as index.jsp). For our basic Struts 2 application, we'll use a minimal xml configuration. Note the file name is struts.xml and it should be in the src folder (struts.xml must be on the web application's root class path).
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="basicstruts2" extends="struts-default"> <action name="index"> <result>/index.jsp</result> </action> </package> </struts>
Build and Run the Application