xoxobool

成功者,永远成功,失败者,永远失败,我要面对者,走向成功!

导航

Struts2和Spring的整合

1.Struts和Spring之间的整合,还是配置问题。

2.最重要的是spring是个容器,原来所有的框架的使用是要注入到spring中的啊.... 怪不得,说它是个容器那,原来还真是个容器啊!

3.下面开始配置一下,使用吧,对了使用之前,记得jar包考进去,别忘了了!阿秋!

4.web.xml 选择2.5版的用啦

<?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.容器注入的代码

spring的配置文件app.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: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>

 

posted on 2016-12-22 19:49  春之林木  阅读(148)  评论(0编辑  收藏  举报