这是一个eclipse基于maven整合的struts2-spring-hibernate的简单项目,
1,首先创建一个maven项目如下:名字可以自己命名
,
创建好之后如下图:
会报错,为什么呢?因为这是一个不完整的maven项目,缺少web.xml。
右键点击项目,如下操作,会自动补全web.xml文件
。
补全的如下图所示:
,好了一个完整的maven骨架就好了。
2, 向 pom.xml 导入 ssh所需要的相关 jar包 和 tomcat 7 插件
<!-- 导入 ssh -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.struts2-spring-hibernate.zh</groupId>
<artifactId>struts2-spring-hibernate.zh</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>ssh</name>
<description>这是一个struts2-spring-hibernate的整合</description>
<!-- 导入 ssh -->
<!-- spring orm web test jdbc tx hibernate c3p0 mysql 驱动 struts spring-struts -->
<properties>
<!--spring的版本 -->
<spring.version>3.2.12.RELEASE</spring.version>
<!-- hibernate的版本 -->
<hibernate.version>3.6.10.Final</hibernate.version>
<!-- strtus2的版本 -->
<struts2.version>2.3.15.3</struts2.version>
<!-- slf4j日志所需要的版本 -->
<slf4j.version>1.7.5</slf4j.version>
<!--c3p0连接池的版本 -->
<c3p0.version>0.9.1.2</c3p0.version>
<!--mysql的版本 -->
<mysql.version>5.0.8</mysql.version>
<!--servlet的版本 -->
<servlet.version>2.5</servlet.version>
<!-- jsp的版本 -->
<jsp.version>2.0</jsp.version>
<!--junit单元测试的版本 -->
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<dependency>
<!-- spring -->
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- slf4j日志所需要的版本 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- c3p0连接池 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<!--mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>runtime</scope>
</dependency>
<!-- struts -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<!-- struts 注解开发 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<!--servlet相关 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope><!-- 测试的时候需要 -->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
</project>
3,ssh 下的web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>struts2-spring-hibernate.zh</display-name>
<!--加载本地spring相关配置applicationContext.xml,这里习惯用这个,当然你也可以自己命名如spring.xml-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--spring的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--struts2的核心配置struts2.dispatcher-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!--struts的映射-->
<filter-mapping>
<filter-name>struts2</filter-name>
<!--struts2的映射拦截-->
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
4,log4j日志的配置
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %5p (%c:%L) - %m%n
log4j.rootLogger=info, stdout
5,数据库连接信息配置
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8
user=root
passwd=123456
6,配置applictionContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!--
<context:component-scan base-package="com.demo.usermanager"></context:component-scan>
@Service @Con @Res @Aut @Com
-->
<!--扫描包事务包,这里是扫描的基本包,只要前缀是一样的都会被spring扫描-->
<context:component-scan base-package="com.demo.usermanager"> </context:component-scan>
<!-- 激活jpa 接口注册 spring data jpa 提供实现类,这里是使用的hibernate提供的jpa方法 -->
<jpa:repositories base-package="com.demo.usermanager.dao"/>
<!--sprin容器加载本地jdbc配置-->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--sprin容器配置c3p0连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- entityManagerFactory 作用: 类似sessionFactory 需要注入连接池 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 扫描实体类 实体类注册 -->
<property name="packagesToScan" value="com.demo.usermanager.domain" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="ORACLE" />
<property name="generateDdl" value="true" />
<property name="showSql" value="true" />
<property name="databasePlatform" value="${jdbc.dialect}" />
</bean>
</property>
</bean>
<!-- jpa 事务管理,这里的事务是使用jpa的事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
7,配置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>
<!-- 不用重启服务器 -->
<constant name="struts.i18n.reload" value="true" />
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 表单样式 -->
<constant name="struts.ui.theme" value="simple" />
<!-- 国际化文件 -->
<constant name="struts.custom.i18n.resources" value="messages" />
<!-- 动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- 请求参数的编码方式-->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开-->
<constant name="struts.action.extension" value="action,do,htm,html"/>
<!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.devMode" value="false"/>
<!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 指定由spring负责action对象的创建 -->
<constant name="struts.objectFactory" value="spring" />
<!--
不用注解模式
<package name="book" namespace="/" extends="struts-default">
<action name="login" class="struts2.LoginAction" method="login">
<result name="success">../views/login.jsp</result>
</action>
</package>
-->
<!-- 是否开启动态方法调用-->
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<package name="demo" extends="json-default">
<!--struts的配置顺序: Element : package
Content Model : (result-types?, interceptors?, default-interceptor-ref?, default-action-ref?,
default-class-ref?, global-results?, global-exception-mappings?, action*) -->
<!--登录拦截的配置,interceptors表示拦截器的标签 -->
<!-- <interceptors>
<interceptor name="mylogin" class="cn.itcast.mavenbos.interceptor.LonginInterceptor">
配置需要经过拦截器执行方法 或者不需要拦截方法
excludeMethods这个表示对哪些方法不拦截,includeMethods对哪些 方法进行拦截
<param name="excludeMethods">login</param>
</interceptor>
-->
<!--定义自定义拦截器 -->
<!-- <interceptor-stack name="mystack"> -->
<!--放值栈的信息 -->
<!-- <interceptor-ref name="mystack"></interceptor-ref> -->
<!--默认栈 -->
<!-- <interceptor-ref name="defaultStack"></interceptor-ref> -->
<!-- </interceptor-stack>
</interceptors> -->
<!--启用自定义拦截器,对所有包类有效 -->
<!-- <default-interceptor-ref name="mystack"></default-interceptor-ref> -->
<!--全局结果集 -->
<!-- <global-results> -->
<!--使用重定义redirect地址会改变跳转到指定页面 -->
<!-- <result name="no_login" type="redirect">/login.jsp</result>
</global-results> -->
<!--没有配置class 默认class ActionSupport implements Action SUCCESS转发 -->
<action name="index">
<result>/index.jsp</result>
</action>
</package>
</struts>