工具:myecplise10+struts2.1+hibernate3.3+spring3.0
第一步:新建工程
File-->new-->Web Project:
第二步:搭建struts2框架
引入struts2:
看到此页面,选择好选项后,点击Finish
WebRoot下的lib文件夹中的web.xml会自动配置好,如下
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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></web-app>
struts2配置:
<?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>
<package name="包名" extends="struts-default" namespace="/">
<action name="Action名" class="实例名" method="方法名">
<result name="返回值"></result>
</action>
</package>
</struts>
此时可以随意在struts.xml写一个Action,测试一下是否成功,避免之后错误难以发现。
第三步:
搭建spring3.0
由于需要整合spring和structs2, 添加Struts2-Spring插件包
struts2-spring-plugin-2.1.8.1
选择5个包(此为spring3.0版本,不同版本选项不同)
点击next后,
点击Finish,此时spring已搭建好。
web.xml配置添加监听器,如下:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
web.xml完整配置为:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 指定Spring配置路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- Spring 启动 IOC 容器的 ServletContextListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Strut2 启动 过滤器 -->
<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></web-app>
第四部:
点击next直到出现下图,我通过DB driver连接mysql数据库(如果不会,请自行搜索或者手动填入数据,注意根据数据库选择对应的驱动)
点击next显示如下图,由于通过spring整合,所以不用建立Session工程类,点击Finish
点击完成,至此,基本框架已搭建好。
第五步 建包,配置
由于是MVC,又由spring整合,所以要建立抽象类和实现类,如com.soft.dao中的UserDao只有抽象方法,com.soft.dao.impl的实UserDaoImpl类现接口,实现方法。
很多同学因为spring配置出错,下面给出applicationContext配置代码:
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
>
<--数据库配置-->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/sshshixun">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</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">
true
</prop>
</props>
</property>
<!-- 数据库表映射文件配置 -->
<list>
<value>com/soft/beans/Test1.hbm.xml</value></list>
</property></bean>
<bean id="userDAO" class="com.soft.dao.impl.UserDaoImpl" scope="singleton">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- bean配置 -->
<property name="userDao" ref="userDAO"></property></bean>
<!-- Spring AOP 事务管理配置 -->
<!-- 1. 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 2. 配置事务属性,编写切面通知,对哪些方法进行增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- tx:method 配置切入点方法
name:方法名称,*表示任意方法,xxxx*表示以xxxx开头的方法;
propagation:设置传播行为;
isolation:设置隔离级别;
read-only:是否只读
-->
<tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="query*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- 3. 配置事务切点, 并把切点和事务属性关联起来,配置到Service层 -->
<aop:config>
<!-- 3.1 定义切入点 -->
<aop:pointcut id="txPointcut" expression="execution(* com.soft.service..*.*(..))" />
<!-- 3.2关联切入点与增强的通知 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
<!-- action配置 -->
<bean id="userAction" class="com.soft.action.UserAction" scope="singleton">
<property name="userService" ref="userService"></property>
</bean>
</beans>
配置好struts.xml后,ssh整合可以使用。