Struts Spring Integration

Struts Spring integration is a type of integration common to the struts framework. The different types of Struts integration methods are given below.

  1. Struts Spring Integration
  2. Struts Tiles Integration
  3. Struts Hibernate

1)      Struts Spring Integration: – Spring has a function as an MVC implementation like strut. Strut as the foundation for building quality software under strict deadlines. The spring architecture allows you to connect Struts as your Web framework to Spring-based business and persistence layers. There are three ways to integrate Strut MVC into spring framework.

A)    Use Spring’s ActionSupport class to integrate Struts: – For integrating Struts with spring we create a spring context manually is the most intuitive way.  The org.springframework.web.struts.ActionSupport class provides a getWebApplicationContext() method to easily obtain a spring context. All you need to do is extend your action from spring’s ActionSupport instead of the Struts Action class, as shown below example.

Eg: –

package j2eebrain.books.actions;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.springframework.context.ApplicationContext;
import org.springframework.web.struts.ActionSupport;
import j2eebrain.books.beans.Book;
import j2eebrain.books.business.BookService;
public class SearchSubmit extends ActionSupport {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
DynaActionForm searchForm = (DynaActionForm) form;
String isbn = (String) searchForm.get(“isbn”);
//the old fashion way
//BookService bookService = new BookServiceImpl();
ApplicationContext ctx =
getWebApplicationContext();
BookService bookService =
(BookService) ctx.getBean(“bookService”);
Book book = bookService.read(isbn.trim());
if (null == book) {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError
(“message.notfound”));
saveErrors(request, errors);
return mapping.findForward(“failure”) ;
}

request.setAttribute(“book”, book);
return mapping.findForward(“success”);
}
}

B)    Override the Struts RequestProcessor with Spring’s DelegatingRequestProcessor:-  For this overriding method we use three steps.

1)      To override the Struts RequestProcessor processor with the org.springframework.web.struts.DelegatingRequestProcessor class,as shown below.

Eg: –

<?xml version=“1.0” encoding=“ISO-8859-1” ?>
<!DOCTYPE struts-config PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 1.1//EN”
“http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd”>
<struts-config>
<form-beans>
<form-bean name=“searchForm”
type=“org.apache.struts.validator.DynaValidatorForm”>
<form-property name=“isbn” type=“java.lang.String”/>
</form-bean>
</form-beans>
<global-forwards type=“org.apache.struts.action.ActionForward”>
<forward   name=“welcome” path=“/welcome.do”/>
<forward   name=“searchEntry” path=“/searchEntry.do”/>
<forward   name=“searchSubmit” path=“/searchSubmit.do”/>
</global-forwards>
<action-mappings>
<action    path=“/welcome” forward=“/WEB-INF/pages/welcome.htm”/>
<action    path=“/searchEntry” forward=“/WEB-INF/pages/search.jsp”/>
<action    path=“/searchSubmit”
type=“j2eebrain.books.actions.SearchSubmit”
input=“/searchEntry.do”
validate=“true”
name=“searchForm”>
<forward name=“success” path=“/WEB-INF/pages/detail.jsp”/>
<forward name=“failure” path=“/WEB-INF/pages/search.jsp”/>
</action>
</action-mappings>
<message-resources parameter=“ApplicationResources”/>
<controller processorClass=“org.springframework.web.struts.
DelegatingRequestProcessor”/>
<plug-in className=“org.apache.struts.validator.ValidatorPlugIn”>
<set-property property=“pathnames”
value=“/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml”/>
</plug-in>
<plug-in className=“org.springframework.web.struts.ContextLoaderPlugIn”>
<set-property property=“csntextConfigLocation” value=“/WEB-INF/beans.xml”/>
</plug-in>
</struts-config>

2)      Next step to register the action in my spring config file.

Eg: –

<?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=“bookService” class=“j2eebrain.books.business.BookServiceImpl”/>
<bean name=“/searchSubmit”
class=“j2eebrain.books.actions.SearchSubmit”> |(1)
<property name=“bookService”>
<ref bean=“bookService”/>
</property>
</bean>
</beans>

3)      The SearchSubmit action exposes a JavaBean property, allowing spring to populate the property at run time. It’s a Struts action with a JavaBean property.

Eg: –

package j2eebrain.books.actions;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import j2eebrain.books.beans.Book;
import j2eebrain.books.business.BookService;
public class SearchSubmit extends Action {
private BookService bookService;
public BookService getBookService() {
return bookService;
}
public void setBookService(BookService bookService)  {
this.bookService = bookService;
}
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
DynaActionForm searchForm = (DynaActionForm) form;
String isbn = (String) searchForm.get(“isbn”);
Book book = getBookService().read(isbn.trim());
if (null == book) {
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError(“message.notfound”));
saveErrors(request, errors);
return mapping.findForward(“failure”);
}
request.setAttribute(“book”, book);
return mapping.findForward(“success”);
}
}

C)    Delegate Struts Action management to the spring framework: – To do this by registering a proxy in the strut-config action mapping. The proxy is responsible for the Struts action in the spring context. Because the action is under spring’s control, it populates the action’s JavaBean properties and leaves the door open to applying features such as spring’s AOP interceptors. Steps for implementing this property is,

1.)    The delegation method of Spring integration

Eg: –

<?xml version=“1.0” encoding=“ISO-8859-1” ?>
<!DOCTYPE struts-config PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 1.1//EN”
“http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd”>
<struts-config>
<form-beans>
<form-bean name=“searchForm”
type=“org.apache.struts.validator.DynaValidatorForm”>
<form-property name=“isbn”    type=“java.lang.String”/>
</form-bean>
</form-beans>
<global-forwards type=“org.apache.struts.action.ActionForward”>
<forward   name=“welcome” path=“/welcome.do”/>
<forward   name=“searchEntry” path=“/searchEntry.do”/>
<forward   name=“searchSubmit” path=“/searchSubmit.do”/>
</global-forwards>
<action-mappings>
<action    path=“/welcome” forward=“/WEB-INF/pages/welcome.htm”/>
<action    path=“/searchEntry” forward=“/WEB-INF/pages/search.jsp”/>
<action    path=“/searchSubmit”
type=“org.springframework.web.struts.DelegatingActionProxy” |(1)
input=“/searchEntry.do”
validate=“true”
name=“searchForm”>
<forward name=“success” path=“/WEB-INF/pages/detail.jsp”/>
<forward name=“failure” path=“/WEB-INF/pages/search.jsp”/>
</action>
</action-mappings>
<message-resources parameter=“ApplicationResources”/>
<plug-in className=“org.apache.struts.validator.ValidatorPlugIn”>
<set-property
property=“pathnames”
value=“/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml”/>
</plug-in>
<plug-in
className=“org.springframework.web.struts.ContextLoaderPlugIn”>
<set-property property=“contextConfigLocation”
value=“/WEB-INF/beans.xml”/>
</plug-in>
</struts-config>

2.)    Registering a Struts action in the spring bean.

Eg: –

<?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=“bookService” class=“j2eebrain.books.business.BookServiceImpl”/>
<bean name=“/searchSubmit”
class=“j2eebrain.books.actions.SearchSubmit”>
<property name=“bookService”>
<ref bean=“bookService”/>
</property>
</bean>
</beans>

Advantages of action-delegation solution

1.)    The strut action has no knowledge of spring and could be used in non spring applications without changing a single line of code. It’s not at a change to the RequestProcessor, and it can take benefits of spring’s AOP features.

2.)    The advantages of action delegation don’t stop there; either once you have your Struts action under spring’s control, you can leverage spring to give them more pizzazz.

Eg: – without spring, all Struts actions must be thread safe. If you set the <bean> tag’s singleton attribute to “false,” however, your application will have a newly minted action object on every request. You might not need this feature, but it’s nice to know you have it in your back pocket.

3.)    Another advantage is we can take the benefits of spring’s lifecycle methods.

Eg: – The <bean> tag’s init-method attribute is used to run a method when the Struts action is instantiated. Similarly, the destroy-method attribute executes a method just before the bean is removed from the container. These methods are a great way to manage expensive objects in much the same way as the Servlet lifecycle does.

We can integrate Struts Spring Hibernate Web Application.

Struts Spring Integration.
Struts Spring Integration.

Struts: – View (User Interface)

Spring: – Service Layer

Hibernate: – DAO layer