struts2+spring+hibernate(SSH)框架的搭建和总结

SSH框架:struts2+spring+hibernate,是目前较流行的一种Web应用程序开源集成框架,用于构建灵活、易于扩展的多层Web应用程序。

   struts2+spring+hibernate架包的下载地址:

   struts2:http://struts.apache.org/ 因为这是国外网站,所以访问的时候会比较慢,请耐心等待

   spring:这个官网现在已经不提供下载了,百度spring-framework-4.3.2.RELEASE,有人会封装好给你下载的

   当然,我也是有准备的:spring-framework-4.3.2.RELEASE;

   hibernate:https://sourceforge.net/projects/hibernate/files/latest/download?source=files  打开这个网址它就会自动下载

   也可以这样下载:http://hibernate.org/orm/downloads/

  具体怎么下载我就不一一细说了,下载完成后,我们找到文件目录。strut2的架包在lib目录的下面,spring的架包在libs目录下面,hibernate的架包在lib\required目录里。

准备好这些包之后,打开eclipse,新建一个项目如(图1).

(图1).

把包导入,当然不是所有的包都需要。struts2所需要的架包如图2.

(图2).

spring所需要的架包如图3.

(图3).

hibernate所需要的架包如图4.

(图4).

这些架包都是我们现在需要的,全部放入项目的lib中。

SSH框架集成从职责上分为4个层:表示层、业务逻辑层、数据持久层和实体层

把JSP页面和包全部新建好。如图5.

(图5).

 

 

 

 

 

 

 

 

 

 

 

 

先把配置文件准备好,

struts2过滤器配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3   <display-name>ZSSH</display-name>
 4   <!-- 这是一只拦路虎 -->
 5   <welcome-file-list>
 6     <welcome-file>index.jsp</welcome-file>
 7   </welcome-file-list>
 8   <filter>
 9               <!-- filter的名字 -->
10               <filter-name>struts2</filter-name>
11               <!-- struts2所需要的驱动类 -->
12               <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
13   </filter>
14   <filter-mapping>
15               <!-- filter过滤的名字 -->
16               <filter-name>struts2</filter-name>
17               <!-- filter过滤的路径 -->
18               <url-pattern>/*</url-pattern>
19   </filter-mapping>
20 </web-app> 

struts.xml文件配置:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <!-- 上面的头,注意版本,从样例里复制过来 showcase.war\WEB-INF\src\java\struts.xml -->
 6 
 7 <struts>
 8     
 9     <!-- 第1步:先定义一个包 -->
10     <package name="mypck001" extends="struts-default">
11         <!-- 第2步:定义一个action,配置跳转信息 name 类似于Servlet @WebServlet("/IndexServlet") 
12             http://xxxx/xxx/Index.action http://xxxx/xxx/Index class 对应于自己写的Action类 当不写method属性时,默认调用的是execute
13     class="ssh.action.IndexAction" ** new ssh.action.IndexAction()
14               设计思想:关心了具体的实现类
15                   必须改为不要关注那个实现类
16              加入spring后,struts的action节点的class属性意义发生变化,
17              直接引用spring帮忙创建的实例
18              -->
19         
20         <action name="Index" class="myIndexAction" method="execute1">
21             <!--
22              跳转是forward
23              /WEB-INF/是防止jsp不经过action就可以访问
24               -->
25             <result name="success">/WEB-INF/jsp/index.jsp</result>
26             <result name="error">/WEB-INF/jsp/error.jsp</result>
27         </action>
28     </package>
29 </struts>
struts001
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <!-- 上面的头,注意版本,从样例里复制过来 showcase.war\WEB-INF\src\java\struts.xml -->
 6 
 7 <struts>
 8     <package name="mypck002" extends="struts-default">
 9         
10     </package>
11 </struts>
struts002
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <!-- 上面的头,注意版本,从样例里复制过来 showcase.war\WEB-INF\src\java\struts.xml -->
 6 
 7 <struts>
 8     <!-- 第1步:先定义一个包 -->
 9     <package name="mypck003" extends="struts-default">
10         
11     </package>
12 </struts>
struts003
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <!-- 上面的头,注意版本,从样例里复制过来 showcase.war\WEB-INF\src\java\struts.xml -->
 6 
 7 <!-- include文件用于分割,实现多人并发不冲突 -->
 8 <struts>
 9     <!-- 告知Struts2运行时使用Spring来创建对象 -->
10     <constant name="struts.objectFactory" value="spring" />
11     <include file="s001.xml" />
12     <include file="s002.xml" />
13     <include file="s003.xml" />
14 </struts>

 Spring配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3   <display-name>ssh_001</display-name>
 4   <welcome-file-list>
 5     <welcome-file>default.jsp</welcome-file>
 6   </welcome-file-list>
 7   <filter>
 8     <filter-name>struts2</filter-name>
 9     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
10   </filter>
11   <filter-mapping>
12     <filter-name>struts2</filter-name>
13     <url-pattern>/*</url-pattern>
14   </filter-mapping>
15     
16 <!-- 确定多个配置文件 -->
17   <context-param>
18   <!-- 参数名为contextConfigLocation -->
19     <param-name>contextConfigLocation</param-name>
20     <!-- 多个配置文件之间以,隔开二 -->
21     <param-value>classpath:applicationContext.xml</param-value>
22   </context-param>
23   <listener>
24   <!-- 采用listener创建Applicat工onContext 实例-->
25     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
26   </listener>
27   </web-app>

 

posted @ 2016-09-05 20:49  可遇丶不可求  阅读(526)  评论(1编辑  收藏  举报