eRedLab-JaNer eRedLab@gmail.com
Keywords: Spring, Jakarta-Struts, WebIDE, MVC, J2EE

Introduction
Spring is a lightweight (size and system point of view of expenditure) of IoC and AOP container. It seeks to simplify the J2EE development or J2EE without EJB. And help the enterprise as a central pillar of development, Spring as a model layer (OR Persistence: Hibernate , JDO, iBatis, etc.) service layer (EJB, JNDI, WebService) and the presentation layer (Struts, JSF, Velocity) have provided a good support and integration. visit the official Spring Station
Jakarta-Struts is the Apache Software Foundation provides an open source project. It provides a Java Web application based on the Model-View-Controller of the MVC framework, in particular, apply to the development of large scalable Web applications. Although the MVC framework for Java-based forms and styles, In fact the MVC model of Spring also offers Web-driven application layer, but Jakarta-Struts is still the framework of all these the best. visit the official Station Struts, I would be how to integrate these two areas of the classic J2EE project given two sets of detailed integration scheme.

Go to the subject
1. First of all, we look at a Spring-Struts integration of applications under the category Action controller source code.
public class CourceAction extends Action (
private CourceService courceService;
public ActionForward execute (
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception (
Set allCources = courceService.getAllCources ();
//.......... the other statements
request.setAttribute ( "cources", allCources);
return mapping.findForward ( "jspView");
))
Analysis: CourceService as a business interface, the interface declaration, a series of business approach. The realization of the method is configured to Spring up and down asked a Bean. So, we all may have a question: Struts action how to obtain a Spring context is included in the Bean this? In order to answer this question, Spring provides two way integration with Struts: (1). Spring from a known base class derived from the context of our own type of Struts Action. Then, in the derived class can be used on super.XX () method to obtain a reference Bean controlled Spring.
(2). To be entrusted to the request of management as a Spring Bean to deal with the Struts Action.
2. Register Spring plug-ins: Struts Action in order to be able to access from Spring Management Bean, we will have to sign up for a Spring application context to know the Struts plug-ins. Struts-config.xml in the following way through to the completion of the registration.
<plug-in classname="org.springframework.web.struts.ContextLoadPlugin">
<set-property value="WEB-INF/Yhcip.xml,......" property="contextConfigLocation">
</ PLUG-IN>
ContextLoadPlugin () is responsible for loading a Spring application context. (Specifically: a WebApplicationContext). Value attribute value for the Spring configuration to load Bean controlled the xml document URI.
3. To complete an integrated program: the realization of a known base class Spring of Action.
This integrated program is a public access to the Spring application context, derived from the base class in all of the Struts Action, but should be glad that: We do not have to go to the preparation of the Spring application context to know the base class, because Spring has provided org . springframework.web.struts.ActionSupport: the realization of an abstract org.apache.struts.action.Action. it is a heavy-duty setServlet () method ContextLoaderPlugin obtained from WebapplicationContext. Therefore, any time we need to call super.getBean ( ) method of a Spring context to get a reference Bean.
Action we will look for some source code:
public class CourceAction extends ActionSupport (
public ActionForward execute (
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception (
/ / Get Spring Context
ApplicationContext context = super.getWebApplicationContext ();
/ / Get CourceService Bean CourseService courseService = (CourseService) context.getBean ( "courseService");
Set allCources = courceService.getAllCources (); request.setAttribute ( "cources", allCources);
//.......... the other statements.
return mapping.findForward ( "jspView");
))
Analysis: The Class Action derived from ActionSupport, when the need for a Spring controlled CourceAction when Bean: it first calls the base class getWebApplicationContext () method to obtain a Spring application context of the quote; then it call getBean () method to obtain from Spring Management of a reference courceService Bean.
Summary
So far, we have the first option with the successful completion of the Spring and Struts integration work. The benefits of this integration is simple and intuitively easy to use. In addition to the need ActionSupport derived from, and the need to obtain from the application context of Bean, the with other non-Spring in the preparation of Struts in Action and configuration similar to the method. However, this integration also has a negative side. The most prominent are: the Action category, we will provide direct access to specific types of Spring, it would be our Struts Action (that is, the control layer) of the code and close coupling with Spring. This is our reluctance to see. In addition, Action is also responsible for category management to find by the Spring of the Bean, which is contrary to the inversion of control (IoC) principle.
4. The realization of the second integrated program: commissioned agents and Action.
This integrated program requires us to write a Struts Action, but it is just a Spring application context is included in the Struts Action, a real agent. The agent Action from the Struts plug-in access to the application context ContextLoaderPlugIn from Struts Action to find the real , and then entrusted to deal with real Struts Action. elegant this approach is that: only agents will include action to deal with specific Spring. real Action can be used as a sub-category org.apache.struts.Action to write.
Now we look for some way in the integration of the source code for Struts Action:
public class CourceAction extends Action (
private CourceService courceService;
public ActionForward execute (
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception (
Set allCources = courceService.getAllCources ();
request.setAttribute ( "cources", allCources);
//.......... the other statements.
return mapping.findForward ( "jspView");
)
/ * Inject CourceService * /
public void setCourceService (CourceService courceService) (
this.courceService = courceService;
))
Analysis: We can see that in this way, we Struts Action classes and the Spring is a low-coupling, which depend only on the Spring to provide inversion of control (IoC) mechanism CourceService into in our Action. To Here, I am sure you have a question: Spring is in the end is how to reverse the control of the provision of this IoC? answer this question, we need two steps to complete the configuration:
(1). In the struts-config.xml to register Struts Action. Yet it must be noted here that the agent registration of Action. Fortunately, we do not have to prepare this type of person. Because Spring has passed org.springframework.web. struts.DelegatingActionProxy provide the Agent Action. specific configuration is as follows:
<action type="org.springframework.web.struts.DelegatingActionProxy" path="/listCourses">
(2) will be true as a Struts Action in Spring Bean and Spring context configuration file as a registered Bean. And Action to be cited into courceService to it.
<bean name="/listCourses">
<property name="">
<ref bean="courseService">
</ property>
</ bean>
Note: name attribute value is very important, it must be and the struts-config.xml in exactly the same path attributes. DelegatingActionProxy This is because the path attribute value will be used in the Spring context to find a real Action. The use of the benefits is that we DelegatingActionProxy can not use any Spring-specific class to write Struts Action. At the same time, Struts action to take advantage of IoC and made him the target. inadequacies only it is not intuitive, relatively complex configuration. In order to make all the more intuitive action a number of commissioned We can do this integration further improved: the use of requests for commissioned.
5. Commissioned by the use of the request.
In order to enable a more intuitive action commissioned a number of looks, Spring provides a DelegatingRequestProcessor, another request of specialized processors for Spring. The need for struts-config.xml in the following configurations:
<controller processorclass="org.springframework.web.struts.DelegatingRequestProcessor">
This, DelegatingRequestProcessor will tell Struts action requests automatically entrusted to the Spring context to deal with the Action. This allows us to use struts-config.xml in struts action to declare the true type them. For example:
<action type = "com.eRedCIP.web.CourceAction" path = "/ listCourses" <
When received a response to / listCourses request, DelegatingRequestProcessor automatically from the Spring context to find a configuration file called / listCourses the Bean (actually a Struts Action) category.
<action type="com.eRedCIP.web.CourceAction" path="/listCourses">

<br /> Conclusion In today's J2EE development, the emergence of a large number of excellent open-source project available for our choosing. Let J2EE software architect who is also confused by the question of whether or not to introduce open source into which the introduction of open source and how high-performance integration to one of our foundation as a development platform up. In this article, I have according to their own area in the J2EE development experience and learned to combine two of the integration of Spring and Struts open-source projects effectively integrated program. limited due to the author, fault urge each of the Touch of Zen swordsman criticism.