Showing posts with label Struts. Show all posts
Showing posts with label Struts. Show all posts

Wednesday, December 22, 2010

Configuring Struts with Spring

There are 2 ways in which Spring features can be injected in Struts. The common point in all the 2 strategies is that we need to register a Struts plug-in that is aware of the Spring application context. Add the following code to your struts-config.xml to register the plug-in:

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/training-servlet.xml,/WEB-INF/…"/>
</plug-in>

ContextLoaderPlugIn loads a Spring application context (a WebApplication-
Context, to be specific), using the context configuration files listed (commaseparated)
in its contextConfigLocation property.

  • Extending the ActionSupport class instead of Action class




public class ListCourseAction extends ActionSupport {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {

ApplicationContext context = getWebApplicationContext();
CourseService courseService = (CourseService) context.getBean("courseService");
Set allCourses = courseService.getAllCourses();
request.setAttribute("courses", allCourses);
return mapping.findForward("courseList");
}
}
The ActionSupport class extends the Action class and has the added method of getWebApplicationContext() which will return the current applicationContext with the help of which we can get the beans.

  • Using the DelegatingActionProxy

The struts action mapping in the struts-config needs to have the DelegatingActionProxy as the class attribute value

<action path="/listCourses"
type="org.springframework.web.struts.DelegatingActionProxy"/>

and the equivalent mapping in the spring config should be

<bean name="/listCourses"
class="com.springinaction.training.struts.ListCourseAction">
<property name="courseService">
<ref bean="courseService"/>
</property>
</bean>

Tuesday, December 21, 2010

Advantages of Spring MVC over Struts

1. Spring provides a very clean division between controllers, JavaBean models, and views.

2. Spring’s MVC is very flexible. Unlike Struts, which forces your Action and Form objects into concrete inheritance (thus taking away your single shot at concrete inheritance in Java), Spring MVC is entirely based on interfaces. Furthermore, just about every part of the Spring MVC framework is configurable via plugging in your own interface. Of course we also provide convenience classes as an implementation option.

3. Spring, like WebWork, provides interceptors as well as controllers, making it easy to factor out behavior common to the handling of many requests.

4. Spring MVC is truly view-agnostic. You don’t get pushed to use JSP if you don’t want to; you can use Velocity,XLST or other view technologies. If you want to use a custom view mechanism – for example, your own templating language – you can easily implement the Spring View interface to integrate it.

5. Spring Controllers are configured via IoC like any other objects. This makes them easy to test, and beautifully integrated with other objects managed by Spring.

6. Spring MVC web tiers are typically easier to test than Struts web tiers, due to the avoidance of forced concrete inheritance and explicit dependence of controllers on the dispatcher servlet.

7. The web tier becomes a thin layer on top of a business object layer. This encourages good practice. Struts and other dedicated web frameworks leave you on your own in implementing your business objects; Spring provides an integrated framework for all tiers of your application.

8. No ActionForms. Bind directly to domain objects

9. More testable code (validation has no dependency on Servlet API)

10. Struts imposes dependencies on your Controllers (they must extend a Struts class), Spring doesn’t force you to do this although there are convenience Controller implementations that you can choose to extend.

11. Spring has a well defined interface to business layer

12. Spring offers better integration with view technologies other than JSP (Velocity / XSLT / FreeMarker / XL etc.)