Struts2和Spring的整合

1.Struts和Spring之間的整合,仍是配置問題。html

2.最重要的是spring是個容器,原來全部的框架的使用是要注入到spring中的啊.... 怪不得,說它是個容器那,原來還真是個容器啊!java

3.下面開始配置一下,使用吧,對了使用以前,記得jar包考進去,別忘了了!阿秋!web

4.web.xml 選擇2.5版的用啦spring

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Test33</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- spring有關 -->
  <!-- 配置文件 -->
  
  <!-- needed for ContextLoaderListener -->  這個能夠經過快捷方式alt+/+c 就能夠出來啦!
    <context-param>  
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:app.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <!-- 把Spring容器放到全局對象中 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Strtus2的過濾器 -->  這就是Struts在web中須要的過濾器啦!
    <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>
</web-app>

5. 定義的方法,DAO,Service和Action類數據庫

public class JiSQ {

    public double add(double a,double b)
    {
        return (a+b);
    }
}
public class TestDAO {
    // 數據庫鏈接
    private String conn;
    
    public String getConn() {
        return conn;
    }

    public void setConn(String conn) {
        this.conn = conn;
    }

    public String getName()
    {
        return "獲得鏈接="+conn+ "調用了DAO";
    }
}
public class TestService {

    private TestDAO testDAO;
    
    
    public TestDAO getTestDAO() {
        return testDAO;
    }


    public void setTestDAO(TestDAO testDAO) {
        this.testDAO = testDAO;
    }


    public String getDAOName()
    {
    //    TestDAO testDAO = new TestDAO();
        
        return testDAO.getName();
    }
}
public class TestAction {

    private TestService testService;
    
    public TestService getTestService() {
        return testService;
    }

    public void setTestService(TestService testService) {
        this.testService = testService;
    }

    public String test()
    {
        //TestService testService = new TestService();
        
        System.out.println(""+testService.getDAOName());
        return "success";
    }
}

6.容器注入的代碼apache

spring的配置文件app.xmlapp

<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

<bean id="jsq" class="com.hanqi.test.JiSQ"></bean>

<bean id="testDAO" class="com.hanqi.test.TestDAO"> 
    <property name="conn" value="Oracle"></property> -- 注入屬性值
</bean>
<bean id="testService" class="com.hanqi.test.TestService">
    <property name="testDAO" ref="testDAO"></property>
</bean>

<!-- Action類的實例不能是單例的 -->
<bean id="testID" class="com.hanqi.test.TestAction" scope="prototype">
    <property name="testService" ref="testService"></property>
</bean>

</beans>

struts的配置文件框架

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <!-- 覆蓋默認的過濾的擴展名 -->
     <constant name="struts.action.extension" value="do,action,,"></constant>
     
     <!-- 容許調用靜態方法和靜態屬性 -->
     <constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
     
     <!-- 定義包 -->
     <package name="index" extends="struts-default">
     
     <action name="testform" class="testID" method="test">  class == spring容器中注入的<action>類
     <result>view.jsp</result>
     </action>
     
     </package>

</struts>
相關文章
相關標籤/搜索