Spring+struts Integration (second scenario)

Source: Internet
Author: User

In the previous article, we learned that the firstSpringWithStrutsIntegrated scenarios,But the lack of this integration scenario isThe web layer knows about spring, because it needs to be actively looking for objects: beanfactory. Scenario Two is done through a dependency injection approach. spring actionproxy agent . To get beanfactory, find path, then find pathaction, then complete injection

core of Scenario two : the core of Struts is given to Spring to create . Spring and other frameset collections are mainly done in this way .

Scenario two integrated framing diagram :

Spring+struts (second option)

Integration principle: The Business logic object (Struts action) is given to spring for creation. The business logic object is injected after it is created. So there is no dependency lookup.

1 spring and struts are dependent on the package configuration.

*struts

--Copy struts-related Java packages and jstl.

--Configure Actionservlet in Web. Xml.

--Provides struts-config.xml core configuration files.

--Provide struts internationalization resource files, preferably with default internationalization files.

*spring

--Copy Spring related Java package

*spring_home/dist/spring.jar

*spring_home/lib/log4j/log4j-1.2.14.jar

*spring_home/lib/jakarta-commons/commons-logging.jar

*spring_home/lib/aspectj/aspectjrt.jar

*spring_home/lib/aspectj/aspectjweaver.jar

--Provides a spring configuration file.


2 Configure Contextloaderlistener in the Web. xml file to let webserver beanfactory in ServletContext when it is started

The code is as follows:

[HTML]View Plaincopyprint?
  1. <!--find filename--
  2. <context-param>
  3. <!--The name here is fixed dead, a constant in the Contextloader- -
  4. <param-name>contextconfiglocation</param-name>
  5. <param-value>classpath:applicationcontext-*.xml</param-value>
  6. </context-param>
  7. <!--set Listener, create beanfactory at once--
  8. <listener>
  9. <listener-class>org.springframework.web.context.contextloaderlistener</ Listener-class>
  10. </listener>



3 The type attribute of the <action> tag in the Struts-config.xml file needs to be changed to spring's proxy action class: O Rg.springframework.web.struts.DelegatingActionProxy

Agent action: Gets the beanfactory, then extracts the action of the request in the IOC container, and then the dependent object is injected.


4 When the action is given to spring to create, the business logic object must be configured and injected into the action

[HTML]View Plaincopyprint?
  1. <beans
  2. name="/login"
  3. class= "com.tgb.struts.usermgr.web.actions.LoginAction" scope="prototype">
  4. < name= "usermanager" ref="Usermanager"> </Property >
  5. </Bean>


you need to be aware of the details when configuring business logic objects :

1 The Name property must be used , and the value of the Name property must match the value of the Path property of the action tag in the Struts-config.xml file.

2 It is recommended that the Scope= "prototype" action of struts be thread-safe.

Summarize:

These are the spring and Struts Integration Scenario 2, which can be seen by comparing the two scenarios. The second scenario incorporates beanfactory into spring management so that the Web layer does not need to proactively look for objects, but rather through spring's dependency injection.

Spring+struts Integration (second scenario)

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.