Examples of integrating spring and struts

Source: Internet
Author: User
Tags aop definition

This article wants to illustrate how to integrate spring and struts with a simple example.

1.Struts and Spring

Struts represents the implementation of the second type of MVC architecture, and the most important component in struts is the actionservlet,action and Actionform subclasses, and Actionservlet represents controller, who accepts requests based on configuration files and Forward these requests to the corresponding Actionform and action subclasses. Actionform the data entered by the user to the Action,action to invoke the business layer component to complete the necessary operations and finally submit to view. Actionservlet uses a configuration file (Struts-config.xml) to load the definition of the action subclass to accept the user's request, and controller find an action definition to accept the request based on the request URL. The struts widget handles the user request, checks the configuration file, and completes the corresponding action.

Spring is a lightweight container that makes it easy to bind objects with an external XML configuration file, and each object can get a pointer to a dependent object by listing the JavaBean property, making the rest easier by binding the XML configuration file. Dependency Injection (DI) is a very powerful feature, and Spring supports pluggable transaction managers, providing more choices for how things are managed. It integrates the persistence architecture while also providing a unified exception classification, and spring also provides a simple mechanism for aspect-oriented (AOP) programming.

Integration of 2.Struts and spring

There are a number of ways to integrate struts applications into the spring framework, and first spring is clearly designed to solve Jee's real-world problems, such as complexity, low performance, testability, and so on; second, The spring framework includes an AOP implementation that allows you to use aspect-oriented program design techniques; third, the spring framework can manage and coordinate struts very easily; like struts, spring also includes MVC implementations, and two architectures have advantages and disadvantages, Struts is the most important architecture of MVC, and many development teams have learned to rely on Struts to develop high-quality software within a specified timeframe, so the development team prefers to integrate spring functionality into spring MVC; The good news is that the spring architecture allows you to integrate Struts The WEB framework, the spring based business layer, and the persistence layer, our approach is to apply the Actionsupport class in spring to integrate struts.

3. Load the application context

First we need to use the 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 follows (1) As shown:

﹤?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=" 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 "br> 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﹥

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.