小狼最近心血来潮,搭建了一个SSH环境,跟大家分享分享
开发环境:Windows7 ,jdk1.6 myeclispe8.6 tomcat6.0.18 mysql5.0
需要jar文件:
junit:junit.jar
db:mysql-connector-java-5.1.10-bin.jar
hibernate3.5.6:
jstl:
spring2.5.5:
struts2:
工程目录:
配置文件:
web.xml
<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> <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>/*</url-pattern> </filter-mapping>
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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <import resource="applicationContext-db.xml"/> <import resource="applicationContext-user.xml"/> </beans>
applicationContext-db.xml
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <tx:advice transaction-manager="transactionManager" id="tx"> <tx:attributes> <tx:method name="save*" read-only="false"/> <tx:method name="update*" read-only="false"/> <tx:method name="delete*" read-only="false"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* qh.zcy.service.*.*(..))" id="perform"/> <aop:advisor advice-ref="tx" pointcut-ref="perform"/> </aop:config>
applicationContext-user.xml
<bean id="userDao" class="qh.zcy.dao.UserDao"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userService" class="qh.zcy.service.UserService"> <property name="dao" ref="userDao"></property> </bean> <bean id="userAction" class="qh.zcy.action.UserAction" scope="prototype"> <property name="service" ref="userService"></property> </bean>
hibernate.cfg.xml
<?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.connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/zhang </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="hibernate.current_session_context_class">org.springframework.orm.hibernate3.SpringSessionContext</property> <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <mapping resource="qh/zcy/entity/User.hbm.xml" /> </session-factory> </hibernate-configuration>
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <!-- 配置文件改了以后不用重新启动 --> <constant name="struts.devMode" value="true"/> <package name="SSH" extends="struts-default" namespace="/"> <action name="userAction_*" class="userAction" method="{1}User"></action> </package> </struts>
UserAction.jave
package qh.zcy.action; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.opensymphony.xwork2.ActionSupport; import qh.zcy.entity.User; import qh.zcy.service.UserService; @Transactional(propagation=Propagation.REQUIRED) public class UserAction extends ActionSupport{ private UserService service; public UserService getService() { return service; } public void setService(UserService service) { this.service = service; } public String listUser() throws Exception { // TODO Auto-generated method stub System.out.println(service.getUsers()); return null; } public String saveUser() throws Exception { // TODO Auto-generated method stub User user=new User(); user.setPassword("zhang"); user.setUsername("zhang"); service.saveUser(user); return null; } }
至此SSH环境搭建完毕,测试一下:
http://localhost:8080/SSH/userAction_save.action
控制台输出如下:
时间: 2024-11-02 13:35:54