Tuesday, August 6, 2013

Struts 2 Spring Integration Tutorial

In this simple hello world example you will see how to integrate Spring and Struts 2 using the struts2-spring-plugin. By doing this you can utilize the Spring's powerful Dependency Injection feature. To learn more about Dependency Injection refer this example.
First add the org.springframework.web.context.ContextLoaderListener to the web.xml file.
01.<?xml version="1.0" encoding="UTF-8"?>
03.<display-name>Struts2Example14</display-name>
04.<filter>
05.<filter-name>struts2</filter-name>
06.<filter-class>org.apache.struts2.dispatcher.ng.filter. StrutsPrepareAndExecuteFilter</filter-class>
07.</filter>
08.<listener>
09.<listener-class>org.springframework.web.context. ContextLoaderListener</listener-class>
10.</listener>
11.<filter-mapping>
12.<filter-name>struts2</filter-name>
13.<url-pattern>/*</url-pattern>
14.</filter-mapping>
15.<welcome-file-list>
16.<welcome-file>index.jsp</welcome-file>
17.</welcome-file-list>
18.</web-app>
By default the applicationContext.xml file will be used for doing the Spring bean configuration.
1.<?xml version="1.0" encoding="UTF-8"?>
2.<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3.<beans>
4.<bean id="helloWorldClass" class="com.vaannila.HelloWorld" >
5.<property name="message" value="Hello World!" />
6.</bean>
7.</beans>
As you can see we have registered the HelloWorld class and injected the "Hello World!" message to the message attribute using the setter injection method.
All the Struts 2 action configuration goes in the struts.xml file.
01.<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
02. 
03.<struts>
04.<package name="default" extends="struts-default">
05.<action name="helloWorld" class="helloWorldClass">
06.<result name="SUCCESS">/success.jsp</result>
07.</action>
08.</package>
09.</struts>
The only change here is instead of referring the com.vaannila.HelloWorld class directly, we relate it using the bean name given in the spring bean configuration file.
The HelloWorld class is shown below. In the execute() method we simply return "SUCCESS" and themessage attribute is set using setter injection.
01.package com.vaannila;
02. 
03.public class HelloWorld {
04. 
05.private String message;
06. 
07.public String getMessage() {
08.return message;
09.}
10. 
11.public void setMessage(String message) {
12.this.message = message;
13.}
14. 
15.public String execute() {
16.return "SUCCESS";
17.}
18.}
In the index.jsp page we forward the request to the helloWorld action.
1.<META HTTP-EQUIV="Refresh" CONTENT="0;URL=helloWorld.action">
After invoking the execute() method the user will be directed to the success.jsp page. In this page we dispaly the message value.
You need to have the following jar files in the WEB-INF/lib directory.
01.commons-fileupload-1.2.1
02.commons-io-1.3.2
03.commons-logging-1.1
04.freemarker-2.3.13
05.junit-3.8.1
06.ognl-2.6.11
07.struts2-convention-plugin-2.1.6
08.struts2-core-2.1.6
09.xwork-2.1.2
10. 
11.struts2-spring-plugin-2.1.6
12. 
13.antlr-runtime-3.0
14.org.springframework.asm-3.0.0.M3
15.org.springframework.beans-3.0.0.M3
16.org.springframework.context-3.0.0.M3
17.org.springframework.core-3.0.0.M3
18.org.springframework.expression-3.0.0.M3
19.org.springframework.web-3.0.0.M3
20.org.springframework.web.servlet-3.0.0.M3
The directory structure of the example is shown below.

No comments:

Post a Comment