Java Learning Notes (one)--STRUTS2 integration with spring

Source: Internet
Author: User

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

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.