AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



struts-spring: How to integrate struts with spring?

Please give me a sample project of struts2 integration with spring.

struts x 31
spring x 24
Posted On : 2015-07-10 23:42:10.0
profile Garima Gupta - anyforum.in Garima Gupta
596129544530
up-rate
6
down-rate

Answers


The ContextLoaderListener class is used to communicate spring application with struts 2. It must be specified in the web.xml file. and applicationContext.xml should be in WEB-INF.

You need to follow the following steps:
----------------------------------------------------------------------
1) Create struts2 application and add spring jar files.
2) In web.xml file, define ContextLoaderListener class.
3) In struts.xml file, define bean name for the action class.
4) In applicationContext.xml file, create the bean. Its class name should be action class name e.g. in.anyforum.Login and id should match with the action class of struts.xml file (e.g. login).
5) In the action class, define extra property e.g. message.

Example of Spring and Struts 2 Integration:
-------------------------------------------------------------------------------
You need to create following files for simple spring and struts 2 application:

1. index.jsp
2. web.xml
3. struts.xml
4. applicationContext.xml
5. Login.java
6. welcome.jsp
7. error.jsp

1) index.jsp:
------------------------------------
This page gets the name from the user.

<%@ taglib uri="/struts-tags" prefix="s"%>

<s:form action="login">
<s:textfield name="userName" label="UserName"></s:textfield>
<s:submit></s:submit>
</s:form>

2) web.xml:
-----------------------------------
It defines controller class for struts 2 and ContextLoaderListener listener class to make connection between struts2 and spring application.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>

</web-app>

3) struts.xml:
------------------------------------
It defines the package with action and result. Here, the action class name is login which will be searched in the applicationContext.xml file.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="abc" extends="struts-default">
<action name="login" class="login">
<result name="success">welcome.jsp</result>
</action>

</package>

</struts>

4) applicationContext.xml:
---------------------------------------------------------------
It defines a bean with id login. This beans corresponds to the mypack.Login class. It will be considered as the action class here.

Note:
------------------
It should be located inside the WEB-INF directory.

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="login" class="in.anyforum.Login">
<property name="message" value="Welcome Spring"></property>
</bean>

</beans>

5) Login.java:
---------------------------------
It defines two property userName and message with execute method where success is returned.

package in.anyforum;
public class Login {
private String userName,message;

public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String execute(){
return "success";
}
}

6) welcome.jsp:
---------------------------------------
It prints values of userName and message properties.

<%@ taglib uri="/struts-tags" prefix="s"%>

Welcome, <s:property value="userName"/><br/>
${message}

7) error.jsp:
------------------------------
It is the error page. But it is not required in this example because we are not defining any logic in the execute method of action class.

Sorry! Some thing went Wrong...

Click Here to Download Struts-Spring Integration Example Using MyEclipse IDE

Output:
---------------------------
Output Struts-Spring Example
Output Struts-Spring Example

Posted On : 2015-07-11 00:14:01
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188237102
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in