Struts, spring, and hibernate have learned almost the same thing. It is time to learn more about how to use spring to integrate the other two;
How to integrate Spring with Struts (the following technical documents are transferred from the network ):
To load spring context in struts, you need to add the following part to the struts-config.xml file:
<Struts-config>
<Plug-in
Classname = "org. springframework. Web. Struts. contextloaderplugin">
<Set-Property = "contextconfiglocation"
Value = "/WEB-INF/applicationcontext. xml"/>
</Plug-in>
</Struts-config>
Struts plug-in provides a good combination of struts and spring. Through plug-in, we can load spring context. However, loading spring context alone has no practical significance. We should also configure struts actions to be handed over to the spring container for management.
<Action-mappings>
<Action Path = "/login"
Type = "org. springframework. Web. Struts. delegatingactionproxy"
Name = "loginform">
<Forward name = "success" Path = "/Main. jsp"/>
<Forward name = "failure" Path = "/login. jsp"/>
</Action>
there is no difference between the form bean node and the traditional struts configuration, but the action is changed. In the traditional Action node, the type attribute is written into the complete Class Name of the action class. Combined with spring, the delegatingactionproxy provided by spring is used as the type attribute of the action, delegatingactionproxy is also Org. apache. struts. action. A subclass of action, which transfers the call request to the real action implementation. In this way, spring obtains the management right of the Action instance. It schedules the action and provides the required action instance for struts. In this way, you can regard action as a bean of spring and enjoy all spring services, such as dependency injection, instance management, and transaction management.
Configure the following nodes in applicationcontext. xml:
.......
Singleton =" false ">
After the delegate, modify the action attribute in the Struts-config.xml, so that the type value of action does not point to the specific implementation class, unified to the proxy class type = "org. springframework. web. struts. delegatingactionproxy ", in applicationcontext. the bean name value in XML is set to the same as the action PATH value in the Struts-config.xml so that the proxy class can find the corresponding bean in the springcontext environment based on the passed path and return the instance to struts. Because the action is under the control of spring, spring can use all struts functions. Due to the spring inversion control feature, Struts cannot feel the existence of spring at all, and can take advantage of all the advantages of the Spring Action Management framework, such as log interception, data verification, and thread security.
Finally, the configuration of this bean is critical. The bean named "/login" corresponds
<Action Path = "/login"……>
......
</Action>
In this way, the spring bean name is associated with the struts action path. When struts loads the corresponding action, delegatingactionproxy searches for the corresponding bean in spring context based on the passed path attribute, and return the instance to struts. At the same time, we can also see that the "/login" bean contains a userdao reference. Spring will provide userdao instances and transaction management services around userdao at runtime according to the configuration. In this way, for struts development, we can continue the struts development process and enjoy the transaction management service provided by spring. Singleton = "false", another attribute of bean, indicates that the instance acquisition method of action is to re-create each time. This also solves the criticized thread security issue in struts.
So far, the SS combination has integrated struts MVC and bean management and transaction management in spring. If we calculate the hibernate part of userdao, we have gained a comprehensive, mature, efficient, and top-down web development framework.
1