SSH整合使用步骤

由于刚开始学习SSH,其中的配置比较多,为了下次能够快速的进行配置,将SSH整合的过程记录下来,以便下次查阅。

软件环境:MyEclipse 9.0、Struts2.2、Spring3、Hibernate3、tomcat 6x。

具体的步骤如下:

1.   新建一个web项目;

2.   在tomcat服务器配置新建的web项目

a)   在conf下的 server.xml中增加Context节,如下:

<Context path="/ssh" docBase="D: \ssh\WebRoot" reloadable="true"/>

b)   或者直接在MyEclipse中对新建的项目进行部署;

c)   开启tomcat服务器,测试是否配置完成。

3.   配置struts2

a)   添加struts2所需的基本jar到lib目录下,尤其注意不要漏掉struts2-spring-plugin-2.2.1.1.jar包,一共9个,如下:

commons-fileupload-1.2.1.jar

commons-io-1.3.2.jar

commons-logging-1.0.4.jar

freemarker-2.3.16.jar

javassist-3.7.ga.jar 这个包可以从实例项目中获取,lib中并没有

ognl-3.0.jar

struts2-core-2.2.1.1.jar

struts2-spring-plugin-2.2.1.1.jar

xwork-core-2.2.1.1.jar

b)         web.xml中增加struts2的过滤器,配置如下:

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

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"

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

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <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>

c)         src目录下创建struts的配置文件,struts.xml,内容如下:

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

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

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

<struts>

    <package name="struts2" extends="struts-default">      

    </package>

</struts>

4.         配置Spring

a)         使用MyEclipseAdd Spring Capabilities功能导入所需的包,包括AOPCorePersistence Core Web,注意要选择将导入的包拷贝到lib目录下;

b)         取消Enable AOP Builder功能,并选择将配置文件生成到WEB-INF目录下;

c)         web.xml中增加Spring的监听器,配置如下:

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

</listener>

5.         配置Hibernate

a)         使用MyEclipseAdd Hibernate Capabilities功能导入所需的包,导入默认选择的两个包即可,注意要选择将导入的包拷贝到lib目录下;

b)         将项目中所需的数据库驱动包和连接池相关的包导入到lib目录下,这里包括mySqlDBCP相关的三个包;

c)         Spring配置文件中配置datasourceSessionFactory,如下:

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

       destroy-method="close">

       <property name="driverClassName">

           <value>com.mysql.jdbc.Driver</value>

       </property>

       <property name="url">

           <value>jdbc:mysql://localhost:3306/test</value>

       </property>

       <property name="username">

           <value>root</value>

       </property>

       <property name="password">

           <value>root</value>

       </property>

    </bean>

 

    <bean id="sessionFactory"

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

       <property name="dataSource">

           <ref local="dataSource" />

       </property>

       <property name="mappingResources">

           <list>

              <!--<value>com/test/bean/User.hbm.xml</value>-->

           </list>

       </property>

       <property name="hibernateProperties">

           <props>

              <prop key="hibernate.dialect">

                  org.hibernate.dialect.MySQLDialect

              </prop>

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

           </props>

       </property>

    </bean>

6.         启动tomcat服务器,进入默认页面,看是否配置成功。

posted on 2011-04-07 20:38  yingql  阅读(5372)  评论(1编辑  收藏  举报

导航