Struts 2 – Setup a Struts 2 Web Application in Maven

I follow the Struts 2 tutorial and setup a simple Struts 2 application.
Create Struts 2 Web Application Using Maven To Manage Artifacts and To Build The Application
 

I used the struts2-core version 2.2.1 and also added a maven-jetty-plugin for running the web application. But when i run mvn jetty:run, the following error is found.
java.lang.IllegalArgumentException: Javassist library is missing in classpath! Please add missed dependency!

It is found that in struts2-core version 2.2.1, the Javassist dependency was excluded in OGNL. So i have to add this dependency in the pom.xml by myself.
 

So here comes to the project details.
1. Create a new Maven project with the following pom.xml
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.wordpress.ykyuen</groupId>
	<artifactId>struts2-trial</artifactId>
	<packaging>war</packaging>
	<version>1.0</version>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>maven-jetty-plugin</artifactId>
				<version>6.1.24</version>
				<configuration>
					<connectors>
						<connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
							<port>8090</port>
							<maxIdleTime>60000</maxIdleTime>
						</connector>
					</connectors>
					<requestLog implementation="org.mortbay.jetty.NCSARequestLog">
						<filename>target/yyyy_mm_dd.request.log</filename>
						<retainDays>90</retainDays>
						<append>true</append>
						<extended>false</extended>
						<logTimeZone>GMT</logTimeZone>
					</requestLog>
				</configuration>
			</plugin>
		</plugins>
	</build>
	
	<dependencies>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts2-core</artifactId>
			<version>2.2.1</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
		</dependency>
		<dependency>
			<groupId>javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.11.0.GA</version>
		</dependency>
	</dependencies>
</project>

 

2. Put the following files in src/main/resources

  • log4j.dtd (Can be found inside log4j.jar – org/apache/log4j/xml/)
  • log4j.xml
  • struts.xml

log4j.xml

<?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 debug and above log messages -->
     <root>
        <priority value="INFO"/> 
        <appender-ref ref="STDOUT" /> 
     </root> 
    
</log4j:configuration>

 

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>

 

3. Create the following files
src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">
	
<display-name>Struts 2 Trial</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<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>

</web-app>

 

src/main/webapp/index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Basic Struts 2 Application - Welcome</title>
	</head>
	<body>
		<h1>Welcome To Struts 2!</h1>
	</body>
</html>

 

4. The project hierarchy will look like this (You can ignore the META-INF)

 

5. Run mvn jetty:run and go to the URL http://localhost:8090/struts2-trial/index.action in the browser

 

Done =)

About these ads

8 thoughts on “Struts 2 – Setup a Struts 2 Web Application in Maven”

    1. you mean u want to remove the maven-jetty-plugin? probably you can still build the project without any problem because the jetty plugin is for starting the java web application.

      Like

    1. sorry, didn’t get the meaning of your question. do you mean you want to deploy it to Apache? it cannot be done becoz it has to be run on Java app server like tomcat or jetty.

      Like

      1. ya i want to deploy this in Apache-tomcat
        can you brief me the steps to test the struts Action classes along with the interceptors and validation.xml files…?
        that means when testing the Action classes interceptors and validation should happens
        you can give a simple example…
        which jars needs to be used…?

        Like

      2. manual deployment to tomcat can be done by copying the .war to the tomcat webapp folder.

        if you want to automate the deployment, you can take a look on the Maven Tomcat Plugin

        About writing unit test, frankly, i haven’t done it before so i couldn’t give you any suggestions. but the following links maybe useful to u to get start.

        hope they can help. =)

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s