26
Jan
11

Spring 2 Enabling Your Struts 1 Application


This page describes the process of integrating the spring framework into an existing struts 1 project.

This page is an attempt to rewrite an page I wrote a few years ago.

This page contains the complete source and uses Maven 2 to help create the project and manage the dependencies and execute the test environment. Its a good idea that you learn what Maven 2 is about by clicking on the Maven tutorial on the right navigation of this page.

Requirements

  • Maven 2
  • Java 5
  • Basic understanding of struts.
  • Struts 1.3.10

Background

The trick to integrating spring framework is to define the action classes as spring beans and wrap them using a DelegatingActionProxy.

This page follows the same example described in the following blog…

http://www.vaannila.com/struts/struts-example/struts-hello-world-example-1.html

Procedure

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp

For the group id enter: com.test
For the artifactId enter: strutsSpringExample

Answer the rest of the questions using defaults [Hit Enter].

cd to the project’s folder

Next we create the src/main/java folder since this is not done for us using the archetype.

on unix you type:

cd strutsSpringExample

In unix you can type:
mkdir -p src/main/java/com/test

Project Configuration

Modify the pom.xml file. It should look like this…

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.test</groupId>
  <artifactId>strutsSpringExample</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>strutsSpringExample Maven Webapp</name>
  <url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc-struts</artifactId>
			<version>2.5.5</version>
		</dependency> 
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts-core</artifactId>
			<version>1.3.10</version>
		</dependency>
		<dependency>
			<groupId>org.apache.struts</groupId>
			<artifactId>struts-taglib</artifactId>
			<version>1.3.10</version>
		</dependency> 
		<dependency>
			<groupId>org.apache.geronimo.specs</groupId>
			<artifactId>geronimo-jsp_2.1_spec</artifactId>
			<version>1.0.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.geronimo.specs</groupId>
			<artifactId>geronimo-servlet_2.5_spec</artifactId>
			<version>1.2</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>

	<build>
		<finalName>strutsSpringExample</finalName>
		<plugins>          
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.0.2</version>
				<configuration>
				    <source>1.5</source>
				    <target>1.5</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.mortbay.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>7.0.0.pre5</version>
				<configuration>
				<webAppConfig>
					<contextPath>/</contextPath>
				</webAppConfig>
				    <scanIntervalSeconds>2</scanIntervalSeconds>
				</configuration>
			</plugin>    
		</plugins>
	</build>
</project>

The following is a standard web.xml file. Nothing really different here.

src/main/webapp/WEB-INF/web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app> 
	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
		    <param-name>config</param-name>
		    <param-value>
		 /WEB-INF/struts-config.xml
		</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>	 
	</servlet>
	 
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
	<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

The following is a struts configuration file. This is the first change that is done to integrate the spring framework. (see highlighted)

Also note on line 16 the type is specified as DelegatingActionProxy.

src/main/webapp/WEB-INF/struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">
 
<struts-config>

    <form-beans>
        <form-bean name="HelloWorldActionForm"
              type="com.test.HelloWorldActionForm"/>
    </form-beans>

    <action-mappings>
        <action path="/HelloWorld" name="HelloWorldActionForm" 
        	scope="session" 
        	type="org.springframework.web.struts.DelegatingActionProxy">
            <forward name="success" path="/helloWorld.jsp"></forward>
        </action>
    </action-mappings>
 
    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
     <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
    </plug-in>
 
</struts-config>

The following is the main spring configuration file. It gets loaded by the ContextLoaderPlugIn mentioned above. The name of the spring beans MUST match the “path” attribute of each action mentioned in the struts config. This is how the DelegatingActionProxy finds the spring bean that is handling the request.

src/main/webapp/WEB-INF/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>
 
    <bean name="/HelloWorld" class="com.test.HelloWorldAction">
    </bean>
 
</beans>

The following is a standard struts form bean. Nothing here has changed due to spring.

src/main/java/com/test/HelloWorldActionForm.java

package com.test;

public class HelloWorldActionForm extends
           org.apache.struts.action.ActionForm {
 
    private String message;
 
    public HelloWorldActionForm() {
        super();
    }
 
    public String getMessage() {
        return message;
    }
 
    public void setMessage(String message) {
        this.message = message;
    } 
}

The following is a standard struts action. Nothing really different here.

src/main/java/com/test/HelloWorldAction.java

package com.test;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
 
public class HelloWorldAction extends org.apache.struts.action.Action {
 
    private final static String SUCCESS = "success";
 
    public ActionForward execute(ActionMapping mapping,ActionForm form,
           HttpServletRequest request,HttpServletResponse response) throws Exception {
 
		HelloWorldActionForm helloWorldForm = (HelloWorldActionForm) form;
		if(helloWorldForm.getMessage() == null || 
			"".equals(helloWorldForm.getMessage().trim())) {
			helloWorldForm.setMessage("Hello World!");		
		}
				
        return mapping.findForward(SUCCESS);
 
    }
}

The following file allows the web application to redirect the browser to the HelloWorld.do struts action.

No spring spectific changes here either.
src/main/webapp/index.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Your Page Title</title>
<meta http-equiv="REFRESH" content="0;url=<c:url value='/HelloWorld.do'/>"></HEAD>
<BODY>
Optional page text here.
</body>
</html>

The following is a standard JSP that contains a struts form. It posts back to the action that rendered it.

src/main/webapp/helloWorld.jsp

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<html:html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>
<bean:write name="HelloWorldActionForm" property="message" />

<html:form action="/HelloWorld">
	<html:text property="message"/>	
	<html:submit/>
</html:form>
</h1>
</body>
</html:html>

Test the application

The application can be tested using jetty inside the strutsSpringExample project directory

Just type:

mvn clean compile jetty:run

That’s all for now!

Advertisements

0 Responses to “Spring 2 Enabling Your Struts 1 Application”



  1. Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

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


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 78 other followers

January 2011
S M T W T F S
« Dec   Feb »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Blog Stats

  • 822,357 hits

%d bloggers like this: