这是一个采用maven项目管理工具,集成struts2+spring3+hibernate3+mysql的一套框架模板,只是提供集成成功,struts2我采用的是零配置方式的,如果对着stuts2零配置方式不理解的,则回头学习一下。该程序测试时在jboss7 AS上运行成功。但是没有使用jndi数据源,在jboss7中数据源配置需要按照module的方式配置,但是jboss4中数据源则相对简单一点。jboss4中使用spring和struts2集成时会有问题,这里有一个解决的文章可以参考一下:http://blog.csdn.net/zgmzyr/article/details/6893537.我的该框架在tomcat7下也是测试通过的,提示:如果阁下的maven仓库没有这些pom.xml,可以去下载后放到项目的lib目录下也是可以的。
1、这是我maven项目的pom.xml文件
<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>org.veight</groupId> <artifactId>veight</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>veight</name> <description /> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <sourceDirectory>src</sourceDirectory> <resources> <resource> <directory>src</directory> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.2</version> <configuration> <warSourceDirectory>${basedir}/WebRoot</warSourceDirectory> <version>3.0</version> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> <dependencies> <!-- spring 3.2 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.2.0.RELEASE</version> </dependency> <!-- Struts 2 --> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.15.2</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-json-plugin</artifactId> <version>2.3.15.2</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-spring-plugin</artifactId> <version>2.3.15.2</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>2.3.15</version> </dependency> <!-- hibernate 3.6 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.10.Final</version> </dependency> <!-- JPA --> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency> <!-- log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <!-- C3P0 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- mysql5.1.30 驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency> </dependencies> </project>
2、struts2的配置文件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> <!-- 在struts2.3.15 admin!login。action,找不到action的错误 开启动态调用方法 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <!-- 是否显示详细错误信息 --> <constant name="struts.devMode" value="false" /> <!-- 配置使用Spring管理Action --> <constant name="struts.objectFactory" value="spring" /> <!-- 后缀 --> <constant name="struts.action.extension" value="action" /> <!-- 国际化资源文件名称 --> <constant name="struts.custom.i18n.resources" value="i18n" /> <!-- 是否自动加载国际化资源文件 --> <constant name="struts.i18n.reload" value="false" /> <!-- convention类重新加载 --> <constant name="struts.convention.classes.reload" value="false" /> <!-- 浏览器是否缓存静态内容 --> <constant name="struts.serve.static.browserCache" value="true" /> <!-- 上传文件大小限制设置 --> <constant name="struts.multipart.maxSize" value="-1" /> <!-- 主题 --> <constant name="struts.ui.theme" value="simple" /> <!-- 编码 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 结果资源的路径 --> <constant name="struts.convention.result.path" value="/WEB-INF/template/" /> <!-- URL资源分隔符 --> <constant name="struts.convention.action.name.separator" value="_" /> <package name="basePackage" extends="struts-default"> <interceptors> <interceptor-stack name="baseStack"> <interceptor-ref name="exception" /> <interceptor-ref name="alias" /> <interceptor-ref name="servletConfig" /> <interceptor-ref name="i18n" /> <interceptor-ref name="prepare" /> <interceptor-ref name="chain" /> <interceptor-ref name="debugging" /> <interceptor-ref name="scopedModelDriven" /> <interceptor-ref name="modelDriven" /> <interceptor-ref name="fileUpload" /> <interceptor-ref name="checkbox" /> <interceptor-ref name="multiselect" /> <interceptor-ref name="staticParams" /> <interceptor-ref name="actionMappingParams" /> <interceptor-ref name="params"> <param name="excludeParams">dojo\..*,^struts\..*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <!-- 配置方法级别的校验 --> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> <param name="validateAnnotatedMethodOnly">true</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> </interceptor-stack> </interceptors> <!-- 配置默认拦截器栈 --> <default-interceptor-ref name="baseStack" /> <!-- 未到找Action指向页面 --> <default-action-ref name="errorPage" /> <action name="errorPage"> <result type="redirect">/html/error_page_404.html</result> </action> </package> <!-- 后台管理 Action --> <package name="admin" extends="basePackage" namespace="/admin/"> <interceptors> <interceptor-stack name="adminStack"> <interceptor-ref name="baseStack" /> </interceptor-stack> </interceptors> <global-results> <result name="error" type="freemarker">/WEB-INF/template/admin/error.ftl</result> <result name="success" type="freemarker">/WEB-INF/template/admin/success.ftl</result> </global-results> </package> </struts>3、重要spring配置文件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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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"> <!-- 使用注解注入 --> <context:annotation-config /> <!-- JDBC参数配置 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="true"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="ignoreResourceNotFound" value="true" /> <property name="locations"> <list> <value>classpath:/jdbc.properties</value> </list> </property> </bean> <!-- 连接池配置 C3p0--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置hibernate信息 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <!-- 配置Hibernate拦截器,自动填充数据的插入、更新时间 --> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <value> <!-- 设置数据库方言 --> hibernate.dialect=${hibernate.dialect} <!-- 设置自动创建|更新|验证数据库表结构 --> hibernate.hbm2ddl.auto=update <!-- 输出SQL语句到控制台 --> hibernate.show_sql=${hibernate.show_sql} <!-- 格式化输出到控制台的SQL语句 --> hibernate.format_sql=${hibernate.format_sql} <!-- 是否开启二级缓存 --> hibernate.cache.use_second_level_cache=false <!-- 配置二级缓存产品 --> hibernate.cache.provider_class=org.hibernate.cache.OSCacheProvider <!-- 是否开启查询缓存 --> hibernate.cache.use_query_cache=false <!-- 数据库批量查询数 --> hibernate.jdbc.fetch_size=50 <!-- 数据库批量更新数 --> hibernate.jdbc.batch_size=30 </value> </property> <!-- 注入实体对象 --> <property name="annotatedClasses"> <list> <value>org.veight.admin.domain.Admin</value> </list> </property> </bean> <!-- 设置需要进行Spring注解扫描的类包 --> <context:component-scan base-package="org.veight" /> <!-- 配置freemarkerManager --> <bean id="freemarkerManager" class="org.apache.struts2.views.freemarker.FreemarkerManager" /> </beans>4、JDBC的属性文件在spring中要引用,你也可以根据自己的情况直接写在spring的配置文件中,jdbc.properties
#SHOP++ JDBC\u914D\u7F6E\u6587\u4EF6 #Wed Mar 13 22:12:01 CST 2013 jdbc.url=jdbc\:mysql\://localhost\:3306/test?useUnicode\=true&characterEncoding\=UTF-8 jdbc.username=root hibernate.dialect=org.hibernate.dialect.MySQLDialect jdbc.driver=com.mysql.jdbc.Driver hibernate.show_sql=false jdbc.password=123456 hibernate.format_sql=false
log4j.rootCategory=error,stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=[SHOP++] %p [%t] %C.%M(%L) | %m%n log4j.logger.com.opensymphony.xwork2.ognl.OgnlValueStack=ERROR
5、在struts2中我喜欢采用零配置,为了把freemarker也整合进来freemarker.properties
classic_compatible=true whitespace_stripping=true template_update_delay=3600 locale=zh_CN default_encoding=utf-8 url_escaping_charset=utf-8 date_format=yyyy-MM-dd time_format=HH:mm:Ss datetime_format=yyyy-MM-dd number_format=\#0.\#\#\#\#\#
6、工程项目就是veight,顺便说一句我的开发工具是myeclipse2013,所以直接导入应该ibu兼容的,最好是自己创建一个工程,然后复制配置文件到你工程中去。源代码我放在我自己资源中,如果在tomcat7或者在jboss7 as中部署,启动服务器,在Mysql数据库的test库中会生成一张admin的表,则说明成功。
7 、前面忘记家里web.xml的配置文件了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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>veight</display-name> <!-- spring配置文件位置 --> <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> <!-- struts核心控制器 --> <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> </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>
6、源码下载地址,不要积分:源码工程下载地址