自己做了一个SSH的小网站,现在我来记录一下自己在开发中遇到的问题和流程,方便以后查看
首先,用的是struts2的2.5+spring4+Hibernate4集成的SSH框架,用的是oracle数据库,tomcat7做web服务器
Hibernate下载:http://download.csdn.net/detail/liangzi321321/9787604
spring下载:http://download.csdn.net/detail/liangzi321321/9787608
struts2下载:http://download.csdn.net/detail/liangzi321321/9787610
首先struts2所需要的jar包:
struts2-core-2.5.2.jar
struts2-spring-plugin-2.5.2.jar
struts2-config-browser-plugin-2.5.2.jar
struts2-json-plugin-2.5.2.jar(因为我用到了json)
commons-io-2.4.jar
commons-lang.jar
commons-fileupload-1.3.2.jar
freemarker-2.3.23.jar
ognl-3.1.10.jar
javassist-3.20.0-GA.jar
Spring用到的jar包:(因为这是我开发完的,可能jar有点多)
spring-aop-4.1.0.RELEASE.jar
spring-aspects-4.1.0.RELEASE.jar
spring-beans-4.1.0.RELEASE.jar
spring-context-4.1.0.RELEASE.jar
spring-context-support-4.1.0.RELEASE.jar
spring-core-4.1.0.RELEASE.jar
spring-expression-4.1.0.RELEASE.jar
spring-instrument-4.1.0.RELEASE.jar
spring-instrument-tomcat-4.1.0.RELEASE.jar
spring-jdbc-4.1.0.RELEASE.jar
spring-jms-4.1.0.RELEASE.jar
spring-messaging-4.1.0.RELEASE.jar
spring-orm-4.1.0.RELEASE.jar
spring-oxm-4.1.0.RELEASE.jar
spring-test-4.1.0.RELEASE.jar
spring-tx-4.1.0.RELEASE.jar
spring-web-4.1.0.RELEASE.jar
spring-webmvc-4.1.0.RELEASE.jar
spring-websocket-4.1.0.RELEASE.jar
commons-logging-1.1.3.jar
aspectj-1.7.4.jar
aspectjrt-1.7.4.jar
aspectjweaver-1.7.4.jar
Hibernate的jar包:
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.21.Final.jar
hibernate-entitymanager.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
antlr-2.7.7.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
javassist-3.20.0-GA.jar(这个包和struts会相同,取最高版本的)
slf4j-api-1.7.22.jar
slf4j-simple-1.7.22.jar
用jdbc连接数据库还需要classes12.jar这个包
接下来是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_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- Hibeinate OSIV 配置 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>FlushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 添加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>
<!-- 添加struts2的过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.ffour.yixing.action</param-value><!-- 这里是你自己的action层 -->
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>SpringOpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SpringOpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>30</session-timeout><!--这里设置的是超时时间30分钟 -->
</session-config>
<!-- 配置字符集过滤器 -->
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<welcome-file-list>
<welcome-file>WebApp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
然后是struts.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true" />
<constant name="struts.devMode" value="true" />
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.serve.static.browserCache" value="false" />
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!--这个是传递正常字符串数据类型的数据的基本配置 -->
<package name="login" namespace="/" extends="struts-default">
<!--action配置 -->
<action name="login" class="com.ffour.yixing.action.YixingUserInfoLogin" method="login">
<result name="error">/WebApp/error.jsp</result>
<result name="success">/WebApp/index.jsp</result>
</action>
</package>
<!--这个是传递json类型的数据的基本配置 -->
<package name="ajax" namespace="/" extends="json-default">
</package>
</struts>
spring的applicationContext.xml配置:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
default-autowire="byName"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 上下文注释配置 -->
<context:annotation-config />
<context:component-scan base-package="com.ffour.yixing.entity" />
<!-- 引入项目配置属性文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- 数据库驱动类 -->
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<!-- 会话工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<!-- 上下文组件扫描 -->
<context:component-scan base-package="com.ffour.yixing"></context:component-scan>
<!-- 定义事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name="dataSource" ref="dataSource"/>
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="list*" propagation="REQUIRED" read-only="true" />
<tx:method name="load*" propagation="REQUIRED" read-only="true" />
<tx:method name="select*" propagation="REQUIRED" read-only="true" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<!-- 其他方法使用默认的事务设置 -->
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="interceptorPointCuts"
expression="execution(* com.ffour.yixing.service.impl.*Impl.*(..))" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="interceptorPointCuts" />
</aop:config>
</beans>
上面spring的配置中用到了db.properties,下面是db.properties的内容:
database.database=ORACLE
database.driverClassName = oracle.jdbc.driver.OracleDriver
database.username =你的数据库用户名
database.password = 密码
database.url = jdbc:oracle:thin:@localhost:1521:orcl
database.show_sql = true
database.dialect = org.hibernate.dialect.Oracle10gDialect
接下来是Hibernate的配置:
<?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.Oracle10gDialect</property>
<!-- 是否在控制台显示sql -->
<property name="hibernate.show_sql">true</property>
<!-- 是否自动提交事务 -->
<property name="hibernate.connection.autocommit">true</property>
<!-- 数据库批量查询最大数 -->
<property name="hibernate.jdbc.fetch_size">50</property>
<!-- 数据库批量更新,添加,删除操作最大数 -->
<property name="hibernate.jdbc.batch_size">20</property>
<property name="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</property>
<!-- 设置自动创建|更新|验证数据库表结构 -->
<property name="hibernate.hbm2ddl.auto">true</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="defaultAutoCommit">true</property>
<!-- 这里是配置你的实体类的xml的,Hibernate可以由数据库的表可以自动反向生成实体类和配置 -->
<mapping resource="com/ffour/yixing/entity/YSysUserinfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
具体的自动成成实体类可以自行百度,或者可以参考这个,用eclipse自动生成实体类http://blog.csdn.net/wangpeng047/article/details/6877720
我这里都是我自己的一些个配置,如果让你感觉不动了或者哪里有问题,可以加我QQ或者微信1373540697,欢迎指出我的不足,我希望一直完善自己(因为我只是一个菜鸟)
稍后我会上传我用到的一些包,可以供大家下载