pom.xml jetty plugin
. org.eclipse.jetty jetty-maven-plugin 9.4.7.v20170914 /$ CTRL+C 8999 10 src/main/webapp/WEB-INF/web.xml
The above plugin will enable you to run the application using mvn jetty:run
Our next step is to add a simple index.jsp to this web application. Create an index.jsp under src/main/webapp with a title of Basic Struts 2 Application - Welcome and in the body add an h1 heading of Welcome To Struts 2!
index.jsp
language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> charset="UTF-8"> Basic Struts 2 Application - Welcome Welcome To Struts 2!
Run mvn jetty:run to run the application.
In a web browser go to http://localhost:8080/basic-struts/index.jsp. You should see the following:
Now that we know we have a working Java web application, let’s add the minimal required Struts 2 framework Jar files to our web application’s class path. In pom.xml add the following dependency node:
pom.xml dependency node
org.apache.struts struts2-core $
Of course replace the $ with the current Struts 2 version (or define within pom properties ). Maven will get the struts2-core jar and the other jar files struts2-core requires (transitive dependencies).
Beginning with Struts version 2.2.3 you do not need to specify a separate dependency node for javassist.
To see what’s happening under the hood, the example application for this tutorial uses log4j2. Setup a log4j2.xml configuration in the src/main/resources folder which contains the following
log4j2.xml
name="STDOUT" target="SYSTEM_OUT"> pattern="%d %-5p [%t] %C (%F:%L) - %m%n"/> name="com.opensymphony.xwork2" level="debug"/> name="org.apache.struts2" level="debug"/> level="warn"> ref="STDOUT"/>
Note the above log4j2 configuration specifies the console as the log target.
You’ll need to add a dependency node for log4j2 to the pom:
pom.xml log4j dependency node
org.apache.logging.log4j log4j-core $ org.apache.logging.log4j log4j-api $
Using both log4j-core and log4j-api allows to use the latest version of Log4j2 without a clash with version provided by the framework.
Optionally, if using maven bom “bill of materials” in dependencyManagement section for both Struts and log4j2, pom.xml will look like. Note that this way you can omit version line for every used module, and all struts2-* and log4j-* modules are managed to be of the same version. The struts2-bom is available since 2.3.20.
UTF-8 UTF-8 1.8 2.5.14.1 2.10.0 org.apache.struts struts2-bom $ pom import org.apache.logging.log4j log4j-bom $ import pom org.apache.struts struts2-core org.apache.logging.log4j log4j-core
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 how the web.xml may look after adding the filter and filter-mapping nodes. web.xml is to be under src/main/webapp/WEB-INF folder.
web.xml Servlet Filter
id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> Basic Struts2 struts2 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter struts2 /* index
For more information about configuring the deployment descriptor for Struts 2 see Core Developers Guide / web.xml page. Note the url-pattern node value is /* meaning the Struts 2 filter will be applied to all URLs for this web application.
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/main/resources folder ( struts.xml must be on the web application’s root class path).
struts.xml
name="struts.devMode" value="true" /> name="basicstruts2" extends="struts-default"> name="index"> /index.jsp
This minimal Struts 2 configuration file tells the framework that if the URL ends in index.action to redirect the browser to index.jsp .
For more information about the struts.xml configuration file see struts.xml .
Run mvn jetty:run to run the web-application using the jetty maven-plugin.
View the console where you should see numerous debug messages that tell you the Struts 2 framework is being included in the basic-struts2 web application.
Open a web browser and go to http://localhost:8080/basic-struts/index.action (note that’s index.action not index.jsp at the end of the URL). You should see the same web page as when going to http://localhost:8080/basic-struts/index.jsp. View the log messages written to the console and you should find several that discuss index.action and index.jsp :
Struts 2 Log Messages
. 2017-04-17 11:16:01,084 DEBUG [qtp1723848804-22] xwork2.DefaultActionProxy (DefaultActionProxy.java:89) - Creating an DefaultActionProxy for namespace [/] and action name [index] . 2017-04-17 11:16:01,172 DEBUG [qtp1723848804-22] result.ServletDispatcherResult (ServletDispatcherResult.java:131) - Forwarding to location: /index.jsp .
The Struts 2 user mailing list is an excellent place to get help. If you are having a problem getting this Basic Struts 2 application to work search the Struts 2 mailing list. If you don’t find an answer to your problem, post a question on the mailing list.
Return to Getting started | or | onward to Hello World using Struts 2 |
Copyright © 2000-2022 The Apache Software Foundation. Apache Struts, Struts, Apache, the Apache feather logo, and the Apache Struts project logos are trademarks of The Apache Software Foundation. All Rights Reserved.