You can download checkout this complete example project , Basic_Struts2_Mvn, from Google Code - http://code.google.com/p/struts2-examples/downloads/list. After unzipping the download you should have a folder named Basic_Struts2_Mvn. In that folder is a README.txt file that has instructions for building and running this example.(and all the other Getting Started tutorial example projects) from the Struts 2 GitHub repository at https://github.com/apache/struts-examples. The example projects use Maven to manage the artifact dependencies and to build the .war files.
Info |
---|
This tutorial assumes you know how to create a Java web application that uses Maven to manage artifacts and build the web application archive (war) file. |
...
In your Java IDE create a Java web application with a project name of Basicbasic_Struts2_Mvn struts that follows the standard Maven project folder structure. In your pom.xml include the following:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<build> <finalName>Basic<finalName>basic_Struts2_Mvn<struts</finalName> </build> |
Step 2 - Add index.jsp
...
Start up your Servlet container and in a web browser go to http://localhost:8080/Basic_Struts2_Mvn/index.jsp. You should see the following:
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>X.X.X.X</version> </dependency> |
Of course replace the X.X.X.X with the current Struts 2 version. Maven will get the struts2-core jar and the other jar files struts2-core requires (transitive dependencies). (NOTE: Beginning with Struts version 2.2.3 you do not need to specify a separate dependency node for javassist.)
Step 4 - Add Logging
To see what's happening under the hood, the example application for this tutorial uses log4j. You'll need to add to pom.xml a dependency node for the log4j jar file:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
|
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c.%M:%L - %m%n"/>
</layout>
</appender>
<!-- specify the logging level for loggers from other libraries -->
<logger name="com.opensymphony">
<level value="DEBUG" />
</logger>
<logger name="org.apache.struts2">
<level value="DEBUG" />
</logger>
<!-- for all other loggers log only info and above log messages -->
<root>
<priority value="INFO"/>
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
|
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<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>
|
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<?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>
|
...
Open a web browser and go to http://localhost:8080/Basic_Struts2_Mvn/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_Struts2_Mvn/index.jsp. View the log messages written to the console and you should find several that discuss index.action and index.jsp:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
com.opensymphony.xwork2.DefaultActionProxy.debug:57 - Creating an DefaultActionProxy for namespace / and action name index
...
org.apache.struts2.dispatcher.ServletDispatcherResult.debug:57 - Forwarding to location /index.jsp
|
...