SSH 配置 简单留言板Demo

环境:

    系统:WIN8.1 + JDK8

    Jar:Struts 2.3.16 + Spring 4.1.5 + MySQL 5.1.20 + Hibernate 4.2.2

    GitHub:gitHub

目录:WEB-INF 下

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-config.xml</param-value>
    </context-param>

    <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>*.action</url-pattern>
        <url-pattern>*.do</url-pattern>
        <!--<url-pattern>/*</url-pattern>-->
    </filter-mapping>
    
    <welcome-file-list>
        <welcome-file>/MsgBoard/index.action</welcome-file>
    </welcome-file-list>
</web-app>

目录:ClassPath 下

Config.properties

jdbc.Url=jdbc:mysql://127.0.0.1:3306/gbs?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc.Driven=com.mysql.jdbc.Driver
jdbc.Name=root
jdbc.PassWord=root

目录:ClassPath

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
   "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.format_sql">true</property>
    </session-factory>
</hibernate-configuration>

目录:ClassPath

spring-config.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"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <context:annotation-config/>
    <context:component-scan base-package="com.hcj.control"/>
    <context:component-scan base-package="com.hcj.service.imp"/>
    <context:component-scan base-package="com.hcj.dao.imp"/>
    <!--读取配置文件-->
    <context:property-placeholder location="classpath:Config.properties"/>
    <!--数据源-->
    <bean id="dataSource"
          class="com.mchange.v2.c3p0.ComboPooledDataSource"
          destroy-method="close">
        <property name="driverClass" value="${jdbc.Driven}"/>
        <property name="jdbcUrl" value="${jdbc.Url}"/>
        <property name="user" value="${jdbc.Name}"/>
        <property name="password" value="${jdbc.PassWord}"/>
    </bean>

    <!--Session工厂-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--指定配置文件-->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <property name="packagesToScan" value="com.hcj.model.po"/>
    </bean>

    <!--事务配置-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!--事务驱动-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager"/>
    <!--注解事务aop-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

目录:ClassPath 下

struts.xml

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

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
        <!-- 指定由spring负责action对象的创建 整合2s必须 -->
        <constant name="struts.objectFactory" value="spring" />
        <!-- 静态资源映射配置 val为正则表达式 -->
        <constant name="struts.action.excludePattern" value="/static/(.*)" />
        <!-- 相当于普通xml配置中的extends,指定默认的继承包 -->
        <constant name="struts.convention.default.parent.package" value="struts-default"/>
        <!--设置web应用的默认编码-->
        <constant name="struts.i18n.encoding" value="UTF-8"/>
        <!--设置访问action的后缀-->
        <constant name="struts.action.extension" value="action,do"/>
        <!--开发环境flase,浏览器缓存-->
        <constant name="struts.serve.static.browserCache" value="false"/>
        <!-- 指定以那些后缀结尾的java类package包被搜索,以发现注解 -->
        <constant name="struts.convention.package.locators" value="control" />
        <!--开发环境true,struts config 修改后是否重新加载-->
        <constant name="struts.configuration.xml.reload" value="true"/>
        <!--开发环境true,打印详细错误info-->
        <constant name="struts.devMode" value="true"/>
        <!--视图主题-->
        <!-- <constant name="struts.ui.theme" value="simple"/>-->
</struts>


你可能感兴趣的:(SSH 配置 简单留言板Demo)