# Struts 2 Spring

Struts 2 Spring was developed by Rod Johnson in June 2003 on Apache 2.0 version that provides robust and secure applications. The latest version of Spring 4.2.1 was released in September 2015. Struts 2 Spring objects are loosely coupled objects and it can be used for development of real time applications unlike struts and hibernates.here struts can be used for developing front end applications like web applications. Hibernates can be used for developing database related operations. ContextLoaderListener is the main assert of spring frame work to communicate with the other frameworks, this listener will be placed in the web.xml.While integrating spring with Struts 2 required jar files needs to be inserted to the project. Following are the required files which describes how to integrate spring framework with Struts 2.

Following is an example which shows how to do the an application by integrating with spring.

index.jsp

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


error.jsp

Sorry man!

welcome.jsp

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

${message}${link}</font>


web.xml

<?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>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>  

The ContextLoaderListener is used to integrate Spring with other web application.

package mypack;
private static final String UserName = null;

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

{
}
}

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

Here just created the data members and also performed SET and GET methods on those members.
struts.xml

<?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">
<result name="success">welcome.jsp</result>
</action>

</package>

</struts> 

applicationContext.xml
Message and link data has written here.

<?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">

<property name="message" value="Hey man stay tuned to splessons and get stuff."></property>
</bean>

</beans>  

The Application Context is spring’s more best in class compartment. Like BeanFactory it can stack bean definitions, wire beans together and apportion beans upon solicitation. Also it includes more undertaking particular usefulness, for example, the capacity to determine literary messages from a properties document and the capacity to distribute application occasions to intrigued occasion audience members. This compartment is characterized by the org.springframework.context.ApplicationContext interface.

The ApplicationContext incorporates all usefulness of the BeanFactory, it is by and large suggested over the BeanFactory. BeanFactory can in any case be utilized for light weight applications like cell phones or applet based applications.
Output
When compile the program following page will be displayed in the browser.

When click on submit button following message will be displayed.

## Summary

• Struts 2 Spring – The spring framework uses an applicationContext.xml file.
• Struts 2 Spring – applicationContext.xml file will be placed at WEB-INF.