In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to integrate Spring and Struts. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
1.Struts and Spring
Struts represents the implementation of the second type of MVC architecture. The most important components in Struts are ActionServlet,Action and ActionForm subclasses, and ActionServlet represents controller, which accepts requests based on configuration files and forwards them to the corresponding ActionForm and Action subclasses. ActionForm transfers the data entered by the user to Action,Action and invokes the business layer components to complete the necessary operations, and * submits it to view. ActionServlet uses a configuration file (struts-config.xml) to load the definition of the Action subclass to accept the user's request. Based on the request URL, controller finds an action definition to accept the request. The Struts component processes the user's request, checks the configuration file, and completes the corresponding action.
Spring is a lightweight container that makes it easy to bind objects using an external XML configuration file. Each object can get a pointer to the dependent object by listing the JavaBean property, and binding the XML configuration file makes the rest of the work easier. Dependency injection (DI) is a very powerful function. Spring supports pluggable transaction managers and provides more options for transaction management. It integrates the persistence architecture and provides a unified exception classification, and Spring also provides a simple mechanism for AOP programming.
Integration of 2.Struts and Spring
There are many ways to integrate Struts applications into the Spring framework. First of all, Spring is clearly designed to solve the practical problems of JEE, such as complexity, low performance, testability and others. Second, the Spring framework includes an AOP implementation that allows you to use aspect-oriented programming techniques; third, the Spring framework can easily manage and coordinate Struts Like Struts, Spring also includes MVC implementation, and both architectures have their advantages and disadvantages. Struts is the most important architecture of MVC. Many development teams have learned to rely on Struts to develop high-quality software within a specified time frame, so the development team would rather integrate the functions of Spring than switch to Spring MVC. The good news is that the structure of Spring allows you to integrate the Struts Web framework, Spring-based business layer and persistence layer. Our approach is to apply the ActionSupport class in Spring to integrate Struts.
3. Load the context of the application
First, we need to use ContextLoaderPlugin in Spring to load the context of the Spring application for Struts ActionServlet. Simply add plug-in to the struts-config.xml file, as shown in (1) below:
xml version= "1.0" encoding= "ISO-8859-1"? >
"- / / 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= "cardno" 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= "com.infotek.Creditcard.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" / >
< 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" > (1)
< set-property property= "contextConfigLocation" value= "/ WEB-INF/beans.xml" / >
< / plug-in >
< / struts-config >
4. ActionSupport classes that use Spring
To integrate Struts with Spring, it is necessary to create a Spring context. The org.springframework.web.struts.ActionSupport class provides a getWebApplicationContext () method that makes it easy to get the Spring context. All you need to do is extend your action from Spring's ActionSupport instead of the Action class in Struts, as shown below:
Package com.infotek.Creditcard.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 com. Infotek.Creditcard.beans.Creditcard
Import com. Infotek.Creditcard.business.CreditcardService
Public class SearchSubmit extends ActionSupport {| (1)
Public ActionForward execute (ActionMapping mapping,ActionForm form
HttpServletRequest request,HttpServletResponse response)
Throws IOException, ServletException {
DynaActionForm searchForm = (DynaActionForm) form
String isbn = (String) searchForm.get ("cardno")
/ / the old fashion way
/ / CreditcardService creditcardService = new CreditcardServiceImpl ()
ApplicationContext ctx = getWebApplicationContext (); | (2)
CreditcardService creditcardService =
(CreditcardService) ctx.getBean ("creditcardService"); | (3)
CreditCard creditard = CreditCardService.read (cardno.trim ())
If (null = = creditard) {
ActionErrors errors = new ActionErrors ()
Errors.add (ActionErrors.GLOBAL_ERROR,new ActionError ("message.notfound"))
SaveErrors (request, errors)
Return mapping.findForward ("failure")
}
Request.setAttribute ("creditcard", creditcard)
Return mapping.findForward ("success")
}
}
In (1), we create an action; by extending the Spring ActionSupport class instead of the Struts Action class. In (2), we use the getWebApplicationContext () method to get an ApplicationContext; in order to get business services, in (3), we use ApplicationContext to find Spring bean. This technology is very easy to understand, unfortunately it binds Struts's action and Spring framework, if you want to replace Spring you have to rewrite the code, and Struts's action is not under the control of Spring, unfortunately this method can not get the benefits of Spring AOP.
This is the end of the article on "how to integrate Spring and Struts". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.