zoukankan      html  css  js  c++  java
  • java学习笔记(11) —— Struts2与Spring的整合

    1、右键 项目名称 —— MyEclipse —— Add Spring Capabilities

    2、选取 Copy checked Library contents to project folder

    3、建立IService 与 Service 【Spring 同样是面向接口编程,因此需要引入IService】

    public interface ILoginService 
    {
        public boolean isLogin(String username,String password);
    }

    public class LoginService implements ILoginService {
    
        public boolean isLogin(String username, String password) {
            if("james".equals(username) && "james".equals(password))
                return true;
            else
                return false;
        }
    }

    4、Action类引入接口,并通过具体实现调用isLogin 方法

       private ILoginService loginservice;
       
    public void setLoginservice(ILoginService loginservice) {
            this.loginservice = loginservice;
        }
    public String execute() throws Exception { if(loginservice.isLogin(this.getUsername(), this.getPassword())) return SUCCESS; else { return "failer"; } }

     5、配置struts.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
        xmlns="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">
    
    <bean id="ILoginService" class="com.test.service.impl.LoginService"></bean>
    //每有一个请求就生成一个Action的实例
    <bean id="loginAction" class="com.test.action.LoginAction" scope="prototype">
      //loginservice 是LoginAction中引用的对象名称
    <property name="loginservice"> <ref local="ILoginService"/> </property> </bean> </beans>

    6、配置web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="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、引入的jar包

  • 相关阅读:
    【项目】项目41
    【项目】项目40
    【项目】项目39
    【项目】项目38
    【项目】项目37
    15-155. Min Stack
    14-160. Intersection of Two Linked Lists
    13-169. Majority Element
    12-206. Reverse Linked List
    11-215. Kth Largest Element in an Array
  • 原文地址:https://www.cnblogs.com/cklovefan/p/5271772.html
Copyright © 2011-2022 走看看