struts、spring框架的处理过程,具体如何配置(如页面跳转的配置、servlet配置、web.xml的配置)

谢谢!

在MyEclipse中新建Web Project,假定项目名为Demo_SSH.

添加能力:

Add Struts Capabilities

Struts config path使用默认的/WEB-INF/struts-config.xml,Struts Specification 选择Struts 1.2 ,然后给资

源文件名包名,那么OK,点击finish。这时项目中Struts1.2Libraries被加入。因为是三个框架整合,这样添

加Libraries的话会有包的重复导入造成冲突,我们只是给项目添加能力,不需要导包。右击

Struts1.2Libraries选择Build Path àRemove form Build Path.

Add Hibernate Capabilities

Hibernate specification选择3.2,Select the libraries to add to the buildpath Show:有两个Libraries,为了

避免包冲突,都不选。点击NEXT,Hibernate config file:可以选择默认的New,(反正最后也要删除),

Open configuration file after wizard completion没有必要了,无所谓的选项,可以去掉然后NEXT,Specify

database connection details?这里要提一下,如果很熟练SSH配置文件的写法的话完全可以将这个复选框

去掉,如果你不是很熟悉Hibernate的配置文件,并且在MyEclipse的DataBase Explorer下将数据库做了配

置,那么可以在DB Driver下选出你的配置,这样下列所有项目都会被自动填写。NEXT,Create

SessionFactory class?这个复选项不必选了,去掉,然后JAVA Compliance Level选5,Finish.(假如你刚

刚选择了DB Driver,那么可以将hibernate.cfg.xml备份出来,这样诸如数据库方言等信息一会儿在配置

Spring的文件时直接复制就可以了.)hibernate.cfg.xml不需要,删除掉。

Add Spring Capabilities

Spring version选择2.0,Select the libraries to add to the buildpath Show:依旧是两个复选,都不选,

NEXT,Enable AOP Builder可以不需要,Specify new or existing Spring bean configuration file?Bean

configuration type选择new,Folder:src,File:applicationContext.xml,NEXT,然后选中Create Spring

SessionFactory that references ,Bean ID随便给一个就可以,只要以后不会重复就成,例如

“sessionFactory”,Finished.

由于没有导入jar包,applicationContext.xml可能会出错误提示,没关系。

Part B.

将无冲突的三个框架jar包导入项目。

打开web.xml,应该有如下信息:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<init-param>

<param-name>debug</param-name>

<param-value>3</param-value>

</init-param>

<init-param>

<param-name>detail</param-name>

<param-value>3</param-value>

</init-param>

<load-on-startup>0</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

下面我们把Spring的一些使用信息加入:

<context-param>

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

<param-value>classpath:applicationContext.xml</param-value>

</context-param>

<servlet>

<servlet-name>SpringContextServlet</servlet-name>

<servlet-class>

org.springframework.web.context.ContextLoaderServlet

</servlet-class>

<load-on-startup>0</load-on-startup>

</servlet>

<filter>

<filter-name>OpenSessionInView</filter-name>

<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

<init-param>

<param-name>singleSession</param-name>

<param-value>false</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>OpenSessionInView</filter-name>

<url-pattern>*.do</url-pattern>

</filter-mapping>

(加在</servlet-mapping> <welcome-file-list>之间即可)

Spring为Struts提供了一个处理器,在struts-config.xml中加入

<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />(加在</action-mappings>之后)

这样,容器启动的时候读配置文件时就会创建Struts和Spring的上下文对象了。

Part C.

下面把Hibernate整合进来。

我们先前删除了Hibernate的配置文件是因为Hibernate的配置是写在Spring的配置文件applicationContext.xml中的。applicationContext.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"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!—如果用了其他的Spring配置文件,可以这样导入

<import resource="spring-login.xml" />-->

<bean id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="location">

<value>classpath:configure.properties</value>

</property>

</bean>

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

<property name="dataSource">

<ref bean="dataSource"/>

</property>

</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName">

<value>${jdbc.driverClassName}</value>

</property>

<property name="url">

<value>${jdbc.url}</value>

</property>

<property name="username">

<value>${jdbc.username}</value>

</property>

<property name="password">

<value>${jdbc.password}</value>

</property>

</bean>

<bean id="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource">

<ref bean="dataSource" />

</property>

<property name="mappingResources">

<list>

<!—此处用<value>节点写Bean的实体配置文件-->

<!—可以好多好多Hibernate实体配置 -->

</list>

</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

${hibernate.dialect}

</prop>

<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.format_sql">true</prop>

</props>

</property>

</bean>

<bean id="baseDao" class="edu.bhu.Kupid.persist.BaseDaoImpl" abstract="true">

<property name="sessionFactory">

<ref bean="sessionFactory" />

</property>

<property name="ht">

<ref bean="hibernateTemplate" />

</property>

</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">

<property name="sessionFactory">

<ref bean="sessionFactory" />

</property>

</bean>

</beans>

配置中有classpath:configure.properties这个东西。是指搜索configure.propertie文件。此文件需要手工创建,其内容就是些Key-Value,主要写些通用配置,如数据库信息等,以连接MySQL数据库为例:

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/graduation

jdbc.username=root

jdbc.password=Kupid

hibernate.dialect=org.hibernate.dialect.MySQLDialect

这样applicationContext.xml中就可以用${jdbc.driverClassName}形式进行引用。你的数据库驱动,url等

信息如果在添加Hibernate能力时指定了DB Driver并且删除hibernate.cfg.xml前做了备份,上边的信息都能

在这个文件里找到。

现在,Hibernate Struts Spring 三个框架整合完毕。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-05-18
以Struts 1.2为例
将struts、spring框架集成起来,可以在struts-config.xml文件中配置一个<plug-in/>元素将两者集成

页面跳转就是在struts-config.xml文件的<action>元素中添加<forword>元素指定跳转到的页面路径

至于在web.xml中配置servlet,一般会自动生成,但是像使用ajax就必须手动配置
相似回答