现在的位置: 首页 > 综合 > 正文

Struts+Spring搭建

2014年02月18日 ⁄ 综合 ⁄ 共 4314字 ⁄ 字号 评论关闭

前言

 

本文以Tomcat为j2ee容器,数据库为Sqlserver2005进行说明。Struts版本为2.3.15.3,Spring版本为3.2.5

 

Spring简介

 

Spring也是appache下面的一个开源项目,强大的基于 JavaBeans 的采用控制反转(Inversion of Control,IoC)原则的配置管理,使得应用程序的组件更加快捷简易。当然它的用途不仅这些,还包括:面向切面编程、JDBC支持、事务管理等。

 

获得Spring

 

         Spring官网
http://www.springsource.org/
,由于官网改版,找起来可能会比较麻烦,大家可以从这个网站进行下载所需的包:http://repo.springsource.org/release/org/springframework/spring/

新建StrutsSpringDemo

 

引入lib

 

         解压从官网下载的spring-framework-3.2.5.RELEASE-dist.zip包,在lib中挑选出所需的jar包,并引入struts的jar包(关于Struts的搭建请参考“StrutsDemo搭建”文档)。所需包如下图所示:

web.xml配置

 

   在web.xml中新增如下内容:

<!-- spring配置 -->

   
<context-param>

       
<param-name>contextConfigLocation</param-name>

       
<param-value>/WEB-INF/applicationContext.xml/WEB-INF/spring-service.xml</param-value>

   
</context-param>

   
<listener>

       
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

   
</listener>

   
<!-- struts2配置 -->

   
<filter>

       
<filter-name>struts2</filter-name>

       
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

   
</filter>

新建applicationContext.xml

 

   在项目中WEB-INF下新建applicationContext.xml文件,注意此处JDBC配置使用的JNDI配置,大家可以根据具体情况进行更改,里面内容如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

          http://www.springframework.org/schema/beans/spring-beans.xsd"default-autowire="byName">

 

   
<beanid="dataSource"class="org.springframework.jndi.JndiObjectFactoryBean">

       
<propertyname="jndiName">

           
<value>java:comp/env/jdbc/ehrdb</value>

       
</property>

   
</bean>

 

   
<beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate">

       
<propertyname="dataSource">

           
<refbean="dataSource"/>

       
</property>

   
</bean>

 

   
<beanid="springDao"class="org.apache.struts.helloworld.dao.SpringDao">

       
<propertyname="jdbcTemplate">

           
<refbean="jdbcTemplate"/>

       
</property>

   
</bean>

</beans>

新建spring-service.xml

 

在项目中WEB-INF下新建spring-service.xml文件,里面内容如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

          http://www.springframework.org/schema/beans/spring-beans.xsd"default-autowire="byName">

 

   
<!--配置service -->

   
<beanid="springService"class="org.apache.struts.helloworld.service.SpringService"/>

   

</beans>

 

新建struts.xml

 

在SRC目录下新建struts.xml文件,注意:要想实现Spring托管Struts必须在此配置文件中加入<constant name="struts.objectFactory" value="spring"/>这句代码,具体内容如下:

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPE strutsPUBLIC

    "-//ApacheSoftware Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

   
<constantname="struts.objectFactory"value="spring"/>

   
<constantname="struts.devMode"value="false"/>

 

   
<packagename="basicstruts2"extends="struts-default">

 

       
<actionname="index">

           
<result>/index.jsp</result>

       
</action>

 

       
<actionname="hello"

           
class="org.apache.struts.helloworld.action.HelloWorldAction"method="execute">

           
<resultname="success">/HelloWorld.jsp</result>

       
</action>

 

   
</package>

 

</struts>

 

新建SpringDao

 

package org.apache.struts.helloworld.dao;

 

import org.springframework.jdbc.core.JdbcTemplate;

 

publicclass SpringDao
{

    private JdbcTemplate jdbcTemplate;

 

    publicvoid query(){

       
String value = jdbcTemplate.queryForObject("select
password from tb_manager where id=1"
, String.class);

       
System.out.println(value);

    }

 

    publicvoid setJdbcTemplate(JdbcTemplate
jdbcTemplate
){

       
this.jdbcTemplate
= jdbcTemplate;

    }

}

 

新建SpringService

 

package org.apache.struts.helloworld.service;

 

import org.apache.struts.helloworld.dao.SpringDao;

 

publicclass SpringService
{

    private SpringDao springDao;

    publicvoid doSomething(){

       
System.out.println("SpringService
doSomething..."
);

       
springDao.query();

    }

   

    publicvoid setSpringDao(SpringDao
springDao
){

       
this.springDao
= springDao;

    }

}

 

使用Spring

 

     
在action中定义private SpringService springService;并增加set方法,这样就可以在action中直接使用service了。

 

部署运行

 

         将程序部署至tomcat,访问http://localhost:8080/StrutsSpringDemo运行

 

Demo下载地址

 

          以上代码均为部分核心配置,完整demo下载地址如下:http://download.csdn.net/detail/zfz1214/6679927

抱歉!评论已关闭.