Spring, struts consolidation approach

Source: Internet
Author: User
  about spring, struts combines learning. First, the foreword has just contacted Japan a project, uses the frame name is the Northland framework, mainly uses in the following partial Struts, Spring, IBATIS, Velocity. How Struts and Spring are combined. Two, spring offers three ways to consolidate struts: Using Spring's Actionsupport class to consolidate Structs using spring's delegatingrequestprocessor overlay struts Req Uestprocessor delegates Struts Action Management to the spring framework (see Get a better handle on Struts actions, with spring HTTP://WWW-128.IBM.C om/developerworks/java/library/j-sr2.html?ca=drs-tp4105 corresponds to a translation: http://gocom.primeton.com/modules/techresource/ Article504.htm?utm_campaign=searchengine&utm_source=baidu&utm_medium=jjpm&utm_term=spring+struts) Three , I'm only concerned with the third integration approach: This approach is implemented through the two and Struts-related classes provided by Spring: Org.springframework.web.struts. Delegatingactionproxy, Org.springframework.web.struts. Contextloaderplugin. Contextloaderplugin implements the PlugIn interface for Struts as long as the following configurations are available in Struts-config.xml:  
< action     Path = "/searchsubmit" > type = "Ca.nexcel.books.actions.DelegatingActionPro XY "             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 > < plug-in className = "Org.springframework.web.str UTs. Contextloaderplugin ">     < Set-property property =" contextconfiglocation "value ="/web-inf/ Beans.xml "/>   </plug-in >
The Actionservlet load is loaded with Spring-related beans.xml, and one of the things associated with Beans.xml is called Webapplicationcontext (the key in Spring is to get WebApp Licationcontext, this can also be done with Spring to manage the business), in Contextloaderplugin to save webapplicationcontext:string attrname = Getservle Tcontextattributename (); Getservletcontext (). setattribute (Attrname, WAC); Then look at Delegatingactionproxy, it inherits from Struts Action, after struts-config.xml all the action-mapping will point to it, but each action-mapping path In the future, this path is used to distinguish between the classes that need to be executed in Beans.xml. The following code:
Public Actionforward Execute () {Action delegateaction = getdelegateaction (mapping);         return Delegateaction.execute (mapping, form, request, response); }
The delegateaction here is a related class in Beans.xml (Beans.xml also requires the class to inherit from Struts's Action) to see how to get Delegateaction:
Protected Action getdelegateaction (actionmapping mapping) throws Beansexception {Webapplicationcontext WAC = Getwebapplicationcontext (Getservlet (), Mapping.getmoduleconfig ()); String beanname = determineactionbeanname (mapping); Return (Action) Wac.getbean (Beanname, Action.class); }
How to get the Webapplicationcontext:
Wac= (Webapplicationcontext) Actionservlet.getservletcontext (). getattribute (Contextloaderplugin.servlet_context_ PREFIX);
  Servlet_context_prefix is the attrname in the Contextloaderplugin mentioned earlier. Now this principle at a glance, contextloaderplugin in the process of Actionservlet initialization to save for Delegatingactionproxy.   IV, in another article mentioned in the above method Opensessioninview Filter can not use   (reference http://wyyhzc.itpub.net/), this thing I am not familiar with, is not a lot of Spring Things in this way can not be used. Here's another way to get a Spring webapplicationcontext: Configure Contextloaderlistener in Web.xml:
< Context-param > < param-name > contextconfiglocation </param-name > < param-value > /web-inf/beans.xml </param-value > </context-param > < listener > < Listener-class > Org.springframework.web.util.Log4jConfigListener </listener-class > </listener > < listener > < Listener-class > Org.springframework.web.context.ContextLoaderListener </listener-class > </Listener >
The corresponding beans.xml, like the one at the front, Log4jconfiglistener to see the relevant documents without having to manage them first. When the Web service starts, let's go and see what Contextloaderlistener has done:
Webapplicationcontext = Createwebapplicationcontext (ServletContext, parent); Servletcontext.setattribute (Webapplicationcontext.root_web_application_context_attribute, This.context);
The same is saved Webapplicationcontext, but key is root_web_application_context_attribute how to do without contextloaderplugin and only use ContextLoad Erlistener. I modify the org.springframework.web.struts below. Delegatingactionproxy changed it to Ca.nexcel.books.actions. Delegatingactionproxy and modify the code: Modify the Getwebapplicationcontext method
Return Delegatingactionutils.findrequiredwebapplicationcontext (Actionservlet, moduleconfig); Change to the bottom method
ServletContext sc = Actionservlet.getservletcontext (); Webapplicationcontext WAC = null; WAC = Webapplicationcontextutils.getwebapplicationcontext (SC); return WAC;
And in Struts-config.xml the action's type points to its own ca.nexcel.books.actions.   Delegatingactionproxy, PlugIn delete web.xml plus just mentioned Listener, start Tomcat to run all normal. V. I posted the Northland profile.
Struts-config.xml: < action-mappings > < action

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.