An example application to integrate Struts and Spring using the struts annotation. The application is very simple, just shown the way to do the confuguration and hence not implemented variuos features given by the two framework.
First we will do the configuration in the Deployment discriptor to use Struts FilterDispatcher and the spring WebApplicationContext. Below is the web.xml
------------------------------------------------------------------------------------
web.xml
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
now we will define the struts.xml for the basic configuration. This can be moved as per your requirment.
------------------------------------------------------------------------------------
struts.xml
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
Now we will configure the spring webapplicationcontext so that we can integrate the spring with the struts.xml
------------------------------------------------------------------------------------
applicationContext.xml
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
Now we will configure the struts mapping using the annotation in the class file. In the below java class we have annotated the method. The annotation "Action" has been used which tells the application that for which url mapping which method or class has to be executed . In the below class we have defined the Action value as "helloworld", so when the url "/helloworld.action" will be hit the application knows which method it has to execute. We have also defined few more attributes. I mean results, which tells the application once it has executed the class as per the definition which view it has to redirect the control. The last configuration "className" is too important in the StrutsSpring application, As this attribute tells the application to look into the applicationContext of the spring for the particular bean definition defined.
Please note as our application is started the url "/helloworld.action" will be hit, so as per the configuration we have done the control will be coming to the HelloWorldAction.java. Once the control comes here the other configuration plays their part. The configuration "className" done above the method hello() tells the application to look for the name helloworld in the applicationcontext.xml.(the attribute "className" need not be same as url. you can give the same name you will define in the applicationContext.xml). Once the bean definition is seen the control moves to the class defined as per the applicationContext.xml. In our application the control moves to the Testing.java and executes the method hello(). This is because we have defined the configuration above the hello() method in the HelloWorldAction.java.(The controller takes the name of the method above which the configuration is defined and search and execute the same name method in the class defined by the spring bean).
Now if you dont want your spring configuration to overwrite the method defined by the struts, you just hace to remove the configuration "className". In that case the application will execute the method above which the mapping configuration has been defined. In our application that would be hello() method from the HelloWorldAction.java.
------------------------------------------------------------------------------------
HelloWorldAction .java
------------------------------------------------------------------------------------
package com.lnt.struts;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage(value = "showcase")
public class HelloWorldAction extends ActionSupport {
@Action(value="helloworld",results={@Result(location="hello.jsp",name="success"),@Result(location="input.jsp",name="input"),@Result(location="error.jsp",name="error")},className="helloworld")
public String hello(){
System.out.println("inside Helloworld hello");
return "error";
}
public String execute() throws Exception {
System.out.println("inside execute method");
return SUCCESS;
}
}
------------------------------------------------------------------------------------
Testing.java
------------------------------------------------------------------------------------
package com.lnt.struts;
public class Testing {
public String hello(){
System.out.println("inside Testing hello");
return "error";
}
public String execute() throws Exception {
System.out.println("inside Testing execute method");
return "success";
}
}
------------------------------------------------------------------------------------
Now you may be wondering why other methods are been defined if its not used any where...... It is just to let you know how to define the same configuration for a class. For example
@ParentPackage(value = "showcase")
@Action(value="helloworld",results={@Result(location="hello.jsp",name="success"),@Result(location="input.jsp",name="input"),@Result(location="error.jsp",name="error")},className="helloworld")
public class HelloWorldAction extends ActionSupport {
// same code as above except the configuration above the method.
}
When you are defining configuration for the class the same configuration you need to put above the class declaration. Now the application will act the same way it has done for the method but instead of executing the hello() method defined in the class it will execute the execute() method of the class. This is because we have exetended our class to ActionSupport. Now as per the configuration of the "className", the application will decide which method to execute from which class the same way it does for the method configuration.
------------------------------------------------------------------------------------
error.jsp
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
input.jsp
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
hello.jsp
------------------------------------------------------------------------------------
index.jsp
------------------------------------------------------------------------------------
the index.jsp can be avoided by configuring the welcome tag in the web.xml to directly pointing helloworld.action url.
------------------------------------------------------------------------------------
Project Sturcture
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
library required
------------------------------------------------------------------------------------
this jar list is for various other needs. Whole jars may not be needed for this application
First we will do the configuration in the Deployment discriptor to use Struts FilterDispatcher and the spring WebApplicationContext. Below is the web.xml
------------------------------------------------------------------------------------
web.xml
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="starter" 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 - Maven Archetype - Starter</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>
<!-- Filters -->
<filter>
<filter-name>action2-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>action2-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Listeners -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Servlets -->
<servlet>
<servlet-name>jspSupportServlet</servlet-name>
<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>
<!-- Welcome file lists -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
------------------------------------------------------------------------------------
now we will define the struts.xml for the basic configuration. This can be moved as per your requirment.
------------------------------------------------------------------------------------
struts.xml
------------------------------------------------------------------------------------
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.convention.result.path" value="/pages" />
<constant name="struts.convention.action.packages" value="com.lnt.struts" />
<constant name="struts.objectFactory.spring.autoWire" value="auto" />
<package name="showcase" extends="struts-default,json-default"
namespace="/">
</package>
</struts>
------------------------------------------------------------------------------------
Now we will configure the spring webapplicationcontext so that we can integrate the spring with the struts.xml
------------------------------------------------------------------------------------
applicationContext.xml
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="autodetect">
<bean id="helloworld" class="com.lnt.struts.Testing" singleton="false">
</bean>
</beans>
------------------------------------------------------------------------------------
Now we will configure the struts mapping using the annotation in the class file. In the below java class we have annotated the method. The annotation "Action" has been used which tells the application that for which url mapping which method or class has to be executed . In the below class we have defined the Action value as "helloworld", so when the url "/helloworld.action" will be hit the application knows which method it has to execute. We have also defined few more attributes. I mean results, which tells the application once it has executed the class as per the definition which view it has to redirect the control. The last configuration "className" is too important in the StrutsSpring application, As this attribute tells the application to look into the applicationContext of the spring for the particular bean definition defined.
Please note as our application is started the url "/helloworld.action" will be hit, so as per the configuration we have done the control will be coming to the HelloWorldAction.java. Once the control comes here the other configuration plays their part. The configuration "className" done above the method hello() tells the application to look for the name helloworld in the applicationcontext.xml.(the attribute "className" need not be same as url. you can give the same name you will define in the applicationContext.xml). Once the bean definition is seen the control moves to the class defined as per the applicationContext.xml. In our application the control moves to the Testing.java and executes the method hello(). This is because we have defined the configuration above the hello() method in the HelloWorldAction.java.(The controller takes the name of the method above which the configuration is defined and search and execute the same name method in the class defined by the spring bean).
Now if you dont want your spring configuration to overwrite the method defined by the struts, you just hace to remove the configuration "className". In that case the application will execute the method above which the mapping configuration has been defined. In our application that would be hello() method from the HelloWorldAction.java.
------------------------------------------------------------------------------------
HelloWorldAction .java
------------------------------------------------------------------------------------
package com.lnt.struts;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@ParentPackage(value = "showcase")
public class HelloWorldAction extends ActionSupport {
@Action(value="helloworld",results={@Result(location="hello.jsp",name="success"),@Result(location="input.jsp",name="input"),@Result(location="error.jsp",name="error")},className="helloworld")
public String hello(){
System.out.println("inside Helloworld hello");
return "error";
}
public String execute() throws Exception {
System.out.println("inside execute method");
return SUCCESS;
}
}
------------------------------------------------------------------------------------
Testing.java
------------------------------------------------------------------------------------
package com.lnt.struts;
public class Testing {
public String hello(){
System.out.println("inside Testing hello");
return "error";
}
public String execute() throws Exception {
System.out.println("inside Testing execute method");
return "success";
}
}
------------------------------------------------------------------------------------
Now you may be wondering why other methods are been defined if its not used any where...... It is just to let you know how to define the same configuration for a class. For example
@ParentPackage(value = "showcase")
@Action(value="helloworld",results={@Result(location="hello.jsp",name="success"),@Result(location="input.jsp",name="input"),@Result(location="error.jsp",name="error")},className="helloworld")
public class HelloWorldAction extends ActionSupport {
// same code as above except the configuration above the method.
}
When you are defining configuration for the class the same configuration you need to put above the class declaration. Now the application will act the same way it has done for the method but instead of executing the hello() method defined in the class it will execute the execute() method of the class. This is because we have exetended our class to ActionSupport. Now as per the configuration of the "className", the application will decide which method to execute from which class the same way it does for the method configuration.
------------------------------------------------------------------------------------
error.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>Insert title here</title>
</head>
<body>
error
</body>
</html>
------------------------------------------------------------------------------------
input.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>Insert title here</title>
</head>
<body>
input
</body>
</html>
------------------------------------------------------------------------------------
hello.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>Insert title here</title>
</head>
<body>
Hello
</body>
</html>
------------------------------------------------------------------------------------index.jsp
------------------------------------------------------------------------------------
<% response.sendRedirect("helloworld.action"); %>
the index.jsp can be avoided by configuring the welcome tag in the web.xml to directly pointing helloworld.action url.
------------------------------------------------------------------------------------
Project Sturcture
------------------------------------------------------------------------------------
library required
------------------------------------------------------------------------------------
this jar list is for various other needs. Whole jars may not be needed for this application
- antlr-2.7.6.jar
- aopalliance-1.0.jar
- asm-3.3.jar
- asm-commons-3.3.jar
- asm-tree-3.3.jar
- c3p0-0.9.1.jar
- commons-collections-3.1.jar
- commons-fileupload-1.1.1.jar
- commons-io-2.0.1.jar
- commons-lang3-3.1.jar
- commons-logging-1.0.4.jar
- dom4j-1.6.1.jar
- dwr-1.1-beta-3.jar
- freemarker-2.3.19.jar
- hibernate-c3p0-3.6.3.Final.jar
- hibernate-commons-annotations-3.2.0.Final.jar
- hibernate-core-3.6.3.Final.jar
- hibernate-jpa-2.0-api-1.0.0.Final.jar
- javassist-3.11.0.GA.jar
- jta-1.1.jar
- log4j-1.2.16.jar
- mysql-connector-java-5.1.9.jar
- ognl-3.0.5.jar
- sitemesh-2.4.2.jar
- slf4j-api-1.6.4.jar
- slf4j-jcl-1.6.4.jar
- slf4j-log4j12-1.6.4.jar
- spring-2.5.6.jar
- spring-aop-3.0.5.RELEASE.jar
- spring-asm-3.0.5.RELEASE.jar
- spring-beans-3.0.5.RELEASE.jar
- spring-context-3.0.5.RELEASE.jar
- spring-core-3.0.5.RELEASE.jar
- spring-expression-3.0.5.RELEASE.jar
- spring-web-3.0.5.RELEASE.jar
- struts2-config-browser-plugin-2.3.3.jar
- struts2-convention-plugin-2.3.3.jar
- struts2-core-2.3.3.jar
- struts2-sitemesh-plugin-2.3.3.jar
- struts2-spring-plugin-2.3.3.jar
- xwork-core-2.3.3.jar
------------------------------------------------------------------------------------
pom.xml configuration for the above project:
------------------------------------------------------------------------------------
this pom.xml is configured to use Struts 2.3.3 , Spring 3.0.5 , Hibernate 3.6.3 and mysql 5.1
<?xml version="1.0" encoding="UTF-8"?>
<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.lnt</groupId>
<artifactId>struts</artifactId>
<name>StrutsSpringproject</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<struts2.version>2.3.3</struts2.version>
</properties>
<dependencies>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Struts 2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-sitemesh-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-config-browser-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<!-- Servlet & Jsp -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- Jakarta Commons -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.1.1</version>
</dependency>
<!-- Dwr -->
<dependency>
<groupId>uk.ltd.getahead</groupId>
<artifactId>dwr</artifactId>
<version>1.1-beta-3</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.3.Final</version>
</dependency>
<!-- Hibernate c3p0 connection pool -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>3.6.3.Final</version>
</dependency>
<!-- log4j dependency -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jcl</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
<!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
<!-- Strutsjquery plugin dependency start -->
<!-- <dependency> <groupId>com.jgeppert.struts2.jquery</groupId> <artifactId>struts2-jquery-plugin</artifactId>
<version>3.3.1</version> </dependency> <dependency> <groupId>com.jgeppert.struts2.jquery</groupId>
<artifactId>struts2-jquery-grid-plugin</artifactId> <version>3.3.1</version>
</dependency> <dependency> <groupId>com.jgeppert.struts2.jquery</groupId>
<artifactId>struts2-jquery-richtext-plugin</artifactId> <version>3.3.1</version>
</dependency> <dependency> <groupId>com.jgeppert.struts2.jquery</groupId>
<artifactId>struts2-jquery-tree-plugin</artifactId> <version>3.3.1</version>
</dependency> <dependency> <groupId>com.jgeppert.struts2.jquery</groupId>
<artifactId>struts2-jquery-mobile-plugin</artifactId> <version>3.3.1</version>
</dependency> -->
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
------------------------------------------------------------------------------------
project structure as a maven project
------------------------------------------------------------------------------------
No comments:
Post a Comment