简单的步骤内容不做说明了。
第一步下载SSH框架包
Spring:http://www.springsource.org/
Hibernate:http://www.hibernate.org/
Struts2:http://struts.apache.org/2.2.1/index.html
第二部新建一个web 工程
点击web工程
add Spring Capabilities
add struts2 Capabilities
add Hibernate Capabilities
通过myeclipse加入框架,只是为了生成对应的配置文件。 还有就是为了方便后面的开发。不要用myeclipse自带的jar包。不然Jar包冲突就搞死人了。
第三部加入spring hibernate struts2的jar包到lib目录下。
SSH我整理的Jar包我会上传一份附件。 由于附件大小限制我去掉了hibernate3.jar
和spring 下所有jar包 你可以自行添加
但是由于自己经验有限 可能有一些jar是你项目用不到的。但起码没有jar包冲突。
sping 目录下dist所有jar包
hibernate jar 包
hibernate3.jar 以及lib/required/所有jar 不要用hibernate自带的cglib-2.2.jar 跟spring会有jar包冲突
单独下载cglib-nodep-2.2 jar
第四步开始写配置文件
注意新版本的xml 命名空间别弄错了。配置文件不要照抄。配置文件目录结构很重要。我的配置文件放置位置不是默认的。
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> <init-param> <param-name>config</param-name> <param-value> struts-default.xml,com/daqi/modts/configs/struts/struts.xml </param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> <!-- 用于初始化Spring容器的Listener --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param><!-- 配置spring配置文件的路径 --> <param-name>contextConfigLocation</param-name> <param-value> classpath:com/daqi/modts/configs/spring/*-context.xml </param-value> </context-param> <!-- <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <context-param> <param-name>log4jConfigLocation</param-name> <param-value> classpath:configure/configs/log4j.properties </param-value> </context-param> <resource-ref> <res-ref-name>jdbc/net_hos</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>--> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <!-- <error-page> <error-code>500</error-code> <location>/other/error.html</location> </error-page> <error-page> <error-code>404</error-code> <location>/other/error1.html</location> </error-page> --> </web-app>
spring配置文件
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 加载DataBase properties <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:com/daqi/modts/configs/database/jdbc.properties</value> </list> </property> </bean>--> <!--配置数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <!-- 指定连接数据库的驱动 --> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <!-- 指定连接数据库的URL --> <property name="jdbcUrl" value="jdbc:mysql://192.168.1.252:3306/modts?autoReconnect=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8"/> <!-- 指定连接数据库的用户名 --> <property name="user" value="MODTS"/> <!-- 指定连接数据库的密码 --> <property name="password" value="MODTS"/> <!-- 指定连接数据库连接池的最大连接数 --> <property name="maxPoolSize" value="20"/> <!-- 指定连接数据库连接池的最小连接数 --> <property name="minPoolSize" value="1"/> <!-- 指定连接数据库连接池的初始化连接数 --> <property name="initialPoolSize" value="1"/> <!-- 指定连接数据库连接池的连接的最大空闲时间 --> <property name="maxIdleTime" value="20"/> </bean> <!-- Hibernate --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" lazy-init="false"> <property name="dataSource"> <ref local="dataSource" /> </property> <property name="mappingDirectoryLocations"> <list> <value>classpath:com/daqi/modts/model</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop> <prop key="hibernate.cache.use_query_cache">false</prop> <prop key="hibernate.cache.use_second_level_cache">false</prop> </props> </property> </bean> <!-- TransactionManager --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property></bean> <!-- 事务拦截器 --> <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <!-- 下面定义事务传播属性--> <property name="transactionAttributes"> <props> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="modify*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="do*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 事务实例动态代理 --> <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="interceptorNames"> <list> <value>baseTransactionProxy</value> </list> </property> <property name="beanNames"> <value>*Service</value> </property> </bean> <!-- Base JDBC Preferences--> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dataSource" /> </property> </bean> </beans>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <include file="struts-default.xml" /> <package name="loginManage" extends="struts-default"> <action name="LoginAction" class="LoginAction" method="loginUser"> <result name="success">indextemp.jsp</result> <result name="fail">login.jsp</result> </action> <action name="LogOutAction" class="LogOutAction" method="loginOut"> <result name="success">/login.jsp</result> </action> <!-- ajax检测id唯一性 --> <action name="checkUniqueId" class="GeneralAction" method="checkUniqueId"> </action> </package> <package name="studentsManage" extends="struts-default" namespace="/studentsManage"> <action name="studentsInfoBrowse" class="StudentsBrowseAction" method="studentsInfoBrowse"> <result name="success">/studentsManage/studentsBrowse.jsp</result> </action> <action name="studentsAddAction" class="StudentsManageAction" method="addStudents"> <result name="success" type="redirectAction"> <param name="actionName">studentsInfoBrowse</param> <param name="namespace">/studentsManage</param> </result> </action> <package> <!-- spring管理Struts2 Action --> <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" /> <constant name="struts.ui.theme" value="simple"></constant> <constant name="struts.ui.templateDir" value="template"></constant> <!-- <constant name="devMode" value="false"></constant> --> </struts>