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

SSH搭建

2014年01月15日 ⁄ 综合 ⁄ 共 4889字 ⁄ 字号 评论关闭

前言

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

 

新建SSH

 

引入lib

 

         所需包如下图所示:

web.xml配置

 

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

<web-appversion="2.5"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_2_5.xsd">

 

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

 

   
<filter-mapping>

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

       
<url-pattern>/*</url-pattern>

   
</filter-mapping>

   

   
<welcome-file-list>

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

   
</welcome-file-list>

</web-app>

 

新建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">

<!--配置service -->

   
<beanid="managerService"class="org.ssh.service.ManagerService"/>

   

   
<beanid="managerDao"class="org.ssh.dao.ManagerDao"/>

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

 

 

新建hibernate.cfg.xml

 

        
<?xmlversion='1.0'encoding='utf-8'?>

<!DOCTYPEhibernate-configuration PUBLIC

       
"-//Hibernate/HibernateConfiguration DTD 3.0//EN"

       
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

 

<hibernate-configuration>

 

   
<session-factory>

 

       
<!-- Database connection settings -->

       
<!-- 普通连接

       
<propertyname="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>

       
<propertyname="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=ehr_db</property>

       
<propertyname="connection.username">sa</property>

       
<propertyname="connection.password">sa</property>

        
-->

       
<!-- JNDI连接 -->

       
<propertyname="connection.datasource">java:comp/env/jdbc/ehrdb</property>

       

 

       
<!-- JDBC connection pool (use thebuilt-in) -->

       
<propertyname="connection.pool_size">1</property>

 

       
<!-- SQL dialect -->

       
<propertyname="dialect">org.hibernate.dialect.SQLServerDialect</property>

 

       
<!-- Enable Hibernate's automatic sessioncontext management -->

       
<propertyname="current_session_context_class">thread</property>

 

       
<!-- Disable the second-level cache 
-->

       
<propertyname="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

 

       
<!-- Echo all executed SQL to stdout-->

       
<propertyname="show_sql">true</property>

 

       
<!-- Drop and re-create the databaseschema on startup -->

       
<propertyname="hbm2ddl.auto">update</property>

 

       
<mappingresource="org/ssh/model/TbManager.hbm.xml"/>

 

   
</session-factory>

 

</hibernate-configuration>

 

 

新建HibernateUtil

 

     
package org.ssh.dao.util;

 

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

publicclass HibernateUtil
{

 

    privatestaticfinal SessionFactory sessionFactory
= buildSessionFactory();

 

    privatestatic SessionFactory buildSessionFactory(){

       
try{

           
// Create the SessionFactory fromhibernate.cfg.xml

           
returnnew Configuration().configure().buildSessionFactory();

       
}catch(Throwable ex){

           
// Make sure you log the exception, as itmight be swallowed

           
System.err.println("Initial
SessionFactory creationfailed."
+ ex);

           
thrownew ExceptionInInitializerError(ex);

       
}

    }

 

    publicstatic SessionFactory getSessionFactory(){

       
return sessionFactory;

    }

 

}

     

部署运行

 

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

 

Demo下载地址

 

          以上代码均为部分核心配置,完整demo下载地址如下:

http://download.csdn.net/detail/zfz1214/6679953

抱歉!评论已关闭.