A simpler method is used. Instead of integrating struts with spring, Struts is used to call the services provided by spring. This is also the method used in the jpetstore example officially provided by spring. The main advantage of this method is conciseness. In addition, the coupling between struts and spring is very loose. If you want to replace struts with springmvc or other web layer frameworks, there is almost no impact on other layers, this is also one reason for spring's built-in jpetstore to do so. Here we can also choose spring MVC or struts, as long as you change the configuration. However, the disadvantage is that the struts action is not within the manageable scope of spring. That is to say, the action is not a bean in spring. In this way, for the struts action, transactions and other services in spring cannot be used. However, in general, the transaction part should be placed at the business layer as much as possible, and the action should not contain too complex operations and logic. Therefore, this struts and spring integrated application method is feasible for general projects. If you have special requirements, consider several other methods that integrate Spring and struts for spring to manage struts actions.
Create a bascaction, which inherits the action class, and all other custom actions must inherit this baseaction.
/**
*
*/
Package com. ascent. Struts. Action;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import org. Apache. Struts. action. Action;
Import org. Apache. Struts. Action. actionerror;
Import org. Apache. Struts. Action. actionerrors;
Import com. ascent. Bean. customer;
Import com. ascent. Business. ibookservice;
Import com. ascent. Business. icustomerservice;
Import com. ascent. Business. iorderitemservice;
Import com. ascent. Business. iorderservice;
Import com. ascent. util. appcontext;
/**
* @ Author
*
*/
Public class baseaction extends action {
/**
*
*/
Public baseaction (){
Super ();
}
Protected ibookservice getbookservice (){
Return (ibookservice) appcontext. getinstance (). getappcontext (). getbean (
"Bookservice ");
}
Protected iorderservice getorderservice (){
Return (iorderservice) appcontext. getinstance (). getappcontext (). getbean (
"Orderservice ");
}
Protected icustomerservice getcustomerservice (){
Return (icustomerservice) appcontext. getinstance (). getappcontext (). getbean (
"Customerservice ");
}
Protected iorderitemservice getorderitemservice (){
Return (iorderitemservice) appcontext. getinstance (). getappcontext (). getbean (
"Orderitemservice ");
}
Protected string checkuser (httpservletrequest request,
Httpservletresponse response ){
Customer user = NULL;
User = (customer) request. getsession (). getattribute ("user ");
If (user = NULL ){
System. Out. println ("You have no loginning !!!! ");
Actionerrors errors = new actionerrors ();
Errors. Add (actionerrors. global_message, new actionerror ("errors. login "));
This. saveerrors (request, errors );
Return NULL;
} Else {
Return user. getcustname ();
}
}
}
/**
*
*/
Package com. ascent. util;
Import org. springframework. Context. Support .*;
/**
* <P> title: bookstoressh </P>
*
* <P> Description: Bookstore System </P>
*
* <P> copyright: Copyright (c) 2005 </P>
*
* <P> company: ascenttech </P>
*
* @ Author
* @ Version 1.0
*/
Public class appcontext {
Private Static appcontext instance;
Private abstractapplicationcontext appcontext;
Public synchronized static appcontext getinstance (){
If (instance = NULL ){
Instance = new appcontext ();
}
Return instance;
}
Private appcontext (){
This. appcontext = new classpathxmlapplicationcontext (
"Applicationcontext. xml ");
}
Public abstractapplicationcontext getappcontext (){
Return appcontext;
}
}
If struts defines an action inherited from baseaction, you can use the getxxxservice () method to obtain an instance of a service, and then call the business method. Here we actually use a design pattern-service locator ).