一:准备好所需要的包:
spring:可从官网(https://start.spring.io/)下载,使用spring-framework-4.2.2.RELEASE-dist.zip\spring-framework-4.2.2.RELEASE\libs中除去javadoc以及sources结尾的jar包
struts:可从官网(http://struts.apache.org/download.cgi)下载,使用struts-2.5.2-all.zip\struts-2.5.2\lib中的jar包
hibernate:可从官网(http://hibernate.org/orm/)下载,使用hibernate-release-5.2.2.Final\lib\required下的jar包
(有时做项目不一定要用到所有包,但本文不在此详述各包作用,另,本文开发环境jdk1.8,tomcat v8.0,eclipse4.6.0)
二:1.先加入struts,编辑web.xml,加入filter
各版本写法略有不同,请参考包内示例
<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>
然后,编写struts.xml文件,ssh的配置文件统统放在src目录下(不是绝对,但一般放在一起,另,spring配置文件需在web.xml中配置路径,稍后再说)。
必要的头文件以及声明,参考包内示例,之后是<struts>标记
<?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">
<!-- 上面的头,注意版本,从样例里复制过来 showcase.war\WEB-INF\src\java\struts.xml -->
<struts>
<span style="background-color: rgb(51, 255, 51);"><!-- 告知Struts2运行时使用Spring来创建对象 -->
<constant name="struts.objectFactory" value="spring" /></span>
<span style="white-space:pre"> </span><span style="color:#000099;"><!-- 可以不引入xml文件,直接将xml文件写入,这样写的好处在于利于团队合作,减少可能出现的冲突 -->
<include file="s001.xml" />
<include file="s002.xml" />
<include file="s003.xml" /></span>
</struts>
<struts>标记中的基本写法
<pre name="code" class="html"> <!-- 第1步:先定义一个包 -->
<package name="mypck001" extends="struts-default">
<!-- 第2步:定义一个action,配置跳转信息 name 类似于Servlet @WebServlet("/IndexServlet")
http://xxxx/xxx/Index.action http://xxxx/xxx/Index class 对应于自己写的Action类 当不写method属性时,默认调用的是execute
class="ssh.action.IndexAction" ** new ssh.action.IndexAction()
加入spring后,struts的action节点的class属性意义发生变化,原本是包名点action名,变为application.xml中该位置的id值
直接引用spring帮忙创建的实例
-->
<action name="Index" class="<span style="color:#ffcc00;">myIndexAction</span>" method="execute1">
<!--
跳转是forward
/WEB-INF/是防止jsp不经过action就可以访问
-->
<result name="success">/WEB-INF/jsp/index2.jsp</result>
<result name="error">/WEB-INF/jsp/s_tag.jsp</result>
</action>
struts基本写好,
接下来加入spring
先编写web.xml,加入监听器以及上下文路径
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
编写applicationContext.xml文件,在src目录下
头文件以及声明在包内示例拷
<span style="white-space:pre"> </span><!-- id为bean的名字,class为类的位置,即包名点类名,<span style="font-family: Arial, Helvetica, sans-serif;">scope="prototype"表示非单例</span><span style="font-family: Arial, Helvetica, sans-serif;"> --></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><bean id="<span style="color:#ffcc00;">myIndexAction</span>" class="ssh.action.IndexAction" scope="prototype"></span>
<!-- 需要注入实例的属性要设置set()方法setIs(myIndexService) name为属性名,ref为引用的bean的id,同一个类多个实现写多个property即可 -->
<property name="is" ref="myIndexService"/>
</bean>
最后加入hibernate,编写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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/CardDB</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- 每个数据库都有1个 -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="connection.pool_size">5</property><span style="color:#ff6666;"><!-- 这里使用默认的连接池 --></span>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property><span style="color:#ff6666;"><!-- update为更新,没有则创建,create为每次启动都将原表删除,建立新表 --></span>
<mapping class="ssh.entity.BookCard"></mapping> <span style="color:#ff6666;"><!-- 注意实体类的映射,我这里是直接在实体类添加注解 --></span>
</session-factory>
</hibernate-configuration>
最后使用hibernate的sessionFactory的时候,在spring的applicationContext.xml文件中配置
<bean id="mysessionFactoryMysql" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入Hibernate 配置文件路径,前面要加上 classpath:-->
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
框架基本搭建完成。
(学艺不精,个人拙见,不正之处,望各大牛指正,感激不尽!)