1. Right-click Project name--myeclipse--add Spring capabilities
2. Select Copy checked Library contents to project folder
3, the establishment of IService and Service "Spring is also interface-oriented programming, so need to introduce IService"
Public Interface Iloginservice { publicboolean islogin (String username,string password);}
Public class Implements Iloginservice { publicboolean islogin (string Username, string password) { if("James". Equals (username) && "James". Equals (password)) return true ; Else return false ; }}
4. The action class introduces the interface and invokes the IsLogin method through concrete implementation.
PrivateIloginservice Loginservice;
public void Setloginservice (Iloginservice loginservice) {
This.loginservice = Loginservice;
}
PublicString Execute ()throwsException {if(Loginservice.islogin ( This. GetUserName (), This. GetPassword ())) returnSUCCESS; Else { return"Failer"; } }
5. Configuring the Struts.xml File
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><BeanID= "Iloginservice"class= "Com.test.service.impl.LoginService"></Bean>Generates an instance of an action for each request<BeanID= "Loginaction"class= "Com.test.action.LoginAction"Scope= "Prototype">
//loginservice is the name of the object referenced in Loginaction < Propertyname= "Loginservice"> <refLocal= "Iloginservice"/> </ Property></Bean></Beans>
6. Configure Web. xml
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "2.4"xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>/web-inf/classes/applicationcontext.xml</Param-value> </Context-param><Filter> <Filter-name>Struts2</Filter-name> <Filter-class>Org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</Filter-class></Filter><filter-mapping> <Filter-name>Struts2</Filter-name> <Url-pattern>/*</Url-pattern></filter-mapping> <servlet> <Servlet-name>Uploadservlet</Servlet-name> <Servlet-class>Com.test.servlet.UploadServlet</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>Uploadservlet</Servlet-name> <Url-pattern>/uploadservlet</Url-pattern> </servlet-mapping> <Listener> <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class> </Listener></Web-app>
7, the introduction of the jar package
Java Learning Notes (one)--STRUTS2 integration with spring