Java: struts and spring Integration

Source: Internet
Author: User

I haven't written Java for more than a year! Forget it! Now I have a project on hand. Just take a look! And bask in the past! Article 1 Integration of several struts and spring! Open-source frameworks have developed rapidly, and Struts 2 is now popular. This integration is about struts 1.29 and spring 2.0. There are still a lot of enterprise applications in this version hierarchy. They are proven to be stable.

 

Method 1 of struts integration with spring:

Step 1: Load appliationcontext:
Method 1:
Web. xml
<! -- Path of the spring applicationcontext configuration file -->
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value> classpath *: spring/*. xml </param-value>
</Context-param>
<! -- Load spring applicationcontext -->
<Listener>
<Listener-class> org. springframework. Web. Context. contextloaderlistener </listener-class>
</Listener>

Method 2:
Struts-config.xml
<Plug-in classname = "org. springframework. Web. Struts. contextloaderplugin">
<Set-Property = "contextconfiglocation" value = "/WEB-INF/spring-config/applicationcontext. xml"/>
</Plug-in>

Step 2: let action inherit actionsupport
Public class loginaction extends actionsupport {
...
}

Step 3: Call the actionsupport method getwebapplicationcontext to obtain the webapplicationcontext. Use webapplicatiocontext to obtain the service managed by the spring container.

Public class loginaction extends actionsupport {
Public actionforward execute (actionmapping mapping,
Actionform form,
Httpservletrequest request,
Httpservletresponse response) throws exception {
// Obtain the spring Context
Applicationcontext context = getwebapplicationcontext ();
// Retrieve the bean managed by spring
Studentservice Ss = (studentservice) Context. getbean ("studentservice ");
...
}
...
}

Struts integration with spring:
Step 1: Load appliationcontext:
Step 2: Use the action proxy class (delegatingactionproxy): The role of delegatingactionproxy is to find the managed action in the spring container Based on the path
Public class loginaction extends action {
Private studentservice SS;
// General IOC Injection
Public void setstudentservice (studentservice SS ){
This. Ss = SS;
}
...
}
Struts-config.xml
<Action Path = "/liststudents" type = "org. springframework. Web. Struts. delegatingactionproxy"/>
Step 3: configure action in the spring configuration file
Applicationcontext. xml
<Bean name = "SS"...>
</Bean>
<Bean name = "/liststudents" class = "...">
<Property name = "SS">
<Ref bean = "SS"/>
</Property>
</Bean>

Struts integrates spring in three ways:
Step 1: Load appliationcontext:
Step 2: configure the new controller delegatingrequestprocessor
Struts-config.xml
<Action Path = "/liststudents"/>

<Controller processorclass = "org. springframework. Web. Struts. delegatingrequestprocessor"/>

When a/liststudents request is received, delegatingrequestprocessor automatically searches for a bean named/liststudents from the context of the spring application.

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.