Struts2+Spring4+Hibernate4环境搭建

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <!-- 显示名称 -->
    <display-name>SSH Project</display-name>

    <!-- 欢迎首页 -->
    <welcome-file-list>
        <welcome-file>Register.jsp</welcome-file>
    </welcome-file-list>

    <!-- struts2的配置 -->
    <filter>
        <filter-name>SSH</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        <init-param>
            <param-name>fiterConfig</param-name>
            <param-value>classpath:struts.xml</param-value>
        </init-param>
        <!-- 自动扫描action -->
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>com.ssh.action</param-value>
        </init-param>
    </filter>
    <!-- 所有请求都要经过SSH过滤器 -->
    <filter-mapping>
        <filter-name>SSH</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--  配置spring的监听器,用来初始化ApplicationContext对象 -->
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener> 
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param> 

</web-app>

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>
    <!-- 开启使用开发模式,详细错误提示 -->    
    <constant name="struts.devMode" value="true" />    
    <!-- 将对象交给spring管理 -->    
    <constant name="struts.objectFactory" value="spring" />    
    <!-- 指定资源编码类型 -->    
    <constant name="struts.i18n.encoding" value="UTF-8" />     
    <!-- 指定每次请求到达,重新加载资源文件 -->    
    <constant name="struts.i18n.reload" value="false" />    
    <!-- 指定每次配置文件更改后,自动重新加载 -->    
    <constant name="struts.configuration.xml.reload" value="false" />    
    <!-- 默认后缀名 -->    
    <constant name="struts.action.extension" value="action," /> 
</struts>  

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" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!-- 自动扫描与装载bean -->
    <context:component-scan base-package="com.ssh"></context:component-scan>

    <!-- 引入jdbc配置文件 -->
    <context:property-placeholder location="classpath:config.properties"/>

    <!--  配置 数据源(数据库配置) -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- 配置sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <!--配置Hibernate的方言--> 
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <!-- 配置输出sql -->
                <prop key="show_sql">true</prop>
                <prop key="hibernate.jdbc.batch_size">20</prop>
            </props>
        </property>
        <!--自动扫描实体   xx.hbm.xml -->    
        <property name="packagesToScan"  value="com.ssh.domain" />  
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <!-- 用注解来实现事务管理 -->    
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

config.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://主机名:端口/数据库名?characterEncoding=utf8&amp;zeroDateTimeBehavior=convertToNull&amp;autoReconnect=true
jdbc.username=用户名
jdbc.password=密码

action层注解action代码:

package com.ssh.action;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionSupport;
import com.ssh.domain.User;
import com.ssh.service.IUserService;

@Results ({@Result(name="input", location="/Register.jsp"),
           @Result(name="success", location="/Success.jsp"),
           @Result(name="error", location="/Fail.jsp")})
@Namespace("/")
public class UserAction extends ActionSupport {

    private static final long serialVersionUID = 5776638828441628036L;
    private User u;
    @Autowired
    private IUserService userService;

    @Action("register")
    public String execute() {
        try {
            System.out.println(u.toString());
            userService.register(u);
            return SUCCESS;

        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
    }

    public User getU() {
        return u;
    }

    public void setU(User u) {
        this.u = u;
    }
}

目录结构:
目录结构

源代码:

源代码地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值