Three major framework Integration (ii)--spring and STRUTS2_ Project Environment

Source: Internet
Author: User

1, the integration of Spring and STRUTS2 purposes:
Let Spring manage instances of Action (multiple cases);

Easy injection of dependent objects.


Add Struts2-spring-plugin-2.1.8.jar, responsible for spring management object Dependency Injection


2, the integration of the steps:
Spring is used in a Web project to configure a listener to create a ApplicationContext object;
Add Struts2-spring-plugin.jar to get struts2 to take the action object from the Spring container.

3, in the Web.xml

			<!--Configure spring's listener for initializing ApplicationContext objects-->
			<listener>
				<listener-class> org.springframework.web.context.contextloaderlistener</listener-class>
			</listener>
			< context-param>
				<param-name>contextConfigLocation</param-name>
				<param-value> Classpath:applicationcontext*.xml</param-value>
			</context-param>

4, testing, in the Testaction.java

			@Controller
			public  class  testaction  extends  actionsupport{
				@Resource
				Private  testservice  testservice;

				Public  String  execute ()  throws  exception{
					s.o.p ("--------->  testservice:" + Testservice;);
					Return  "Success";
				}
			

5. Adjust Struts.xml

			<package name= "Default" namespace= "/" extends= "Struts-default" >
				<!--Configure the action for the test-->
				< Action name= "Test" class= "testaction" method= "Testservice" >
					<result name= "Success" >/test.jsp</ result>
				</action>
			</package>


Results: Access to Localhost:8080/itcastoa/test.do
If S.O.P ("---------> Testservice:" + testservice;); The testservice in is not NULL, which means success.


Above, the three major framework integration completed.

When you change only the method body, you do not need to republish. Re-publish the method name when you change it.

















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.