Spring+Struts2 整合

利用Spring+Struts2實現列表顯示

編寫DAO

  1. 引入spring開發包和applicationContext.xml

    <!-- spring ioc\aop -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.12.RELEASE</version>
    </dependency>
    
     <!-- dao -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.12.RELEASE</version>
    </dependency>
    <!-- mysql驅動包、連接池 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.0.2</version>
    </dependency>
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>
    
  2. 在Spring中配置DataSource組件

    <!-- 連接池配置 -->
    <bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="root"></property>
        <property name="password" value="1234"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/studyonline"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    </bean>
    
  3. 在Spring中配置JdbcTemplate組件

    <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg index="0" ref="c3p0"></constructor-arg>
    </bean>
    
  4. 根據操作表編寫實體類

    Direction.java

  5. 編寫Dao接口和實現類

    • 接口

      public interface DirectionDao {
          public List<Direction> findAll();
      }
      
    • 實現類(掃描、注入JdbcTemplate)

      @Repository("directionDao")
      public class DirectionDaoImpl implements DirectionDao{
      
          @Autowired
          private JdbcTemplate template;
      
          public List<Direction> findAll() {
              String sql  = "select * from direction";
              RowMapper<Direction> rowMapper = new BeanPropertyRowMapper<Direction>(Direction.class);
              List<Direction> list = template.query(sql, rowMapper);
              return list;
          }
      
      }
      
    • 掃描配置

      <context:component-scan base-package="cn.xdl"/>
      

Struts2和Spring結合

  1. 將Action掃描到Spring容器,注入DAO

    @ParentPackage("struts-default")
    //@Namespace("/")
    @Controller
    @Scope("prototype")
    public class ListAction {
    
        private List<Direction> list;
    
        @Autowired
        private DirectionDao directionDao;
    
        @Action(value="list",results={@Result(location="/list.jsp")})
        public String list(){
            //調用DAO查詢數據表集合
            list = directionDao.findAll();
            return "success";
        }
        //省略set和get
    }
    
  2. 引入struts2-spring-plugin.jar包

    作用:能使Struts2框架訪問Spring容器,從容器中調用Action對象。

    <!-- struts2整合spring -->
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-spring-plugin</artifactId>
      <version>2.3.12</version>
    </dependency>
    
  3. 在web.xml配置ContextLoaderListener

    作用:在服務器啓動時,實例化Spring容器以及內部組件對象。

      <!-- 配置Listener,用於tomcat啓動時實例化Spring容器對象 -->
      <listener>
        <listener-class>
        org.springframework.web.context.ContextLoaderListener
        </listener-class>
      </listener>
      <!-- 指定spring容器的xml配置文件 -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章