Skip to main content

Struts 2 and springs Integration

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.

Step1:

First add the org.springframework.web.context.ContextLoaderListener to the web.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Struts2Example14</display-name>
<!-- Context Configuration locations for Spring XML files -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
</context-param>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.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>


Step2:

By default the applicationContext.xmll file will be used for doing the Spring bean configuration.

<?xml version="1.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.net.champ.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.

Step3:

All the Struts 2 action configuration goes in the struts.xml file.

<!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.objectFactory"
value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>

<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.net.champ.HelloWorld class directly, we relate it using the bean name given in the spring bean configuration file.

Step4:

The HelloWorld class is shown below. In the execute() method we simply return "SUCCESS" and the message attribute is set using setter injection.

package com.net.champ;

public class HelloWorld {

private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public String execute() {
return "SUCCESS";
}
}

Step5:

In the index.jsp page we forward the request to the helloWorld action.
meta equiv="Refresh" content="0;URL=helloWorld.action"
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. struts2-spring-plugin-2.1.6
11. antlr-runtime-3.0
12. org.springframework.asm-3.0.0.M3
13. org.springframework.beans-3.0.0.M3
14. org.springframework.context-3.0.0.M3
15. org.springframework.core-3.0.0.M3
16. org.springframework.expression-3.0.0.M3
17. org.springframework.web-3.0.0.M3
18. org.springframework.web.servlet-3.0.0.M3

Comments

Popular posts from this blog

Sorting an List in Java

// Create a list String[] strArray = new String[] {"z", "a", "C"}; List list = Arrays.asList(strArray); // Sort Collections.sort(list); // C, a, z // Case-insensitive sort Collections.sort(list, String.CASE_INSENSITIVE_ORDER); // a, C, z // Reverse-order sort Collections.sort(list, Collections.reverseOrder()); // z, a, C // Case-insensitive reverse-order sort Collections.sort(list, String.CASE_INSENSITIVE_ORDER); Collections.reverse(list); // z, C, a

Hibernate

Best sites which are very much useful for assist in Hibernate related problems * Hibernate main site * Hibernate Tutorial * Hibernate Forum * Hibernate Wikipedia * Popular Hibernate Books * Hibernate Materials * Relational Persistence for Idiomatic Java

JSF Lifecycle Phases

JSF Lifecycle Phases JSF follows MVC design pattern to handle request-response process. Basically JSF handles three types of requests. Non-Faces Request Generates Faces Response Faces Request Generates Non-Faces Response Faces Request Generates Faces Response In another case of course non-jsf to non-jsf is there but in this case there is no involvement of JSF action here, so this is not a part of jsf lifecycle process. A JavaServer Faces page is represented by a tree of UI components, called a view . When a client makes a request for the page, the life cycle starts. During the life cycle, the JavaServer Faces implementation must build the view while considering state saved from a previous submission of the page. When the client submits a page, the JavaServer Faces implementation must perform several tasks, such as validating the data input of components in the view and converting input data to types specified on the server side. The JavaServer Faces implementation performs a...