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.
<?xml version="0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.worg/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_xsd" id="WebApp_ID"version="5">
<display-name>Struts2Example14</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.strutsdispatcher.ng.filter.
StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<listener>
<listener-class>org.springframework.web.context. ContextLoaderListener</listener-class>
</listener>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
By default the applicationContext.xml file will be used for doing the Spring bean configuration.
<?xml version="0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloWorldClass" class="com.vaannila.HelloWorld" >
<property name="message" value="Hello World!" />
</bean>
</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.
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 0//EN"
"http://struts.apache.org/dtds/struts-dtd">
<struts>
<package name="default" extends="struts-default">
<action name="helloWorld" class="helloWorldClass">
<result name="SUCCESS">/success.jsp</result>
</action>
</package>
</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.
package com.vaannila;
public class HelloWorld {
private String message;
public String
getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String
execute() {
return "SUCCESS";
}
}
In the index.jsp page we forward the request to the helloWorld action.
<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.
commons-fileupload-1
commons-io-2
commons-logging-1
freemarker-13
junit-1
struts2-convention-plugin-6
struts2-core-6
xwork-2
struts2-spring-plugin-6
antlr-runtime-0
org.springframework.asm-M3
org.springframework.beans-M3
org.springframework.context-M3
org.springframework.core-M3
org.springframework.expression-M3
org.springframework.web-M3
org.springframework.web.servlet-M3
The directory
structure of the example is shown below.
No comments:
Post a Comment