【笔记——JAVA WEB】S2SH的初步接触

其实S2SH并没有想象中那么难,这是我接触后的感想。我在我的一个校园招聘项目中使用了SSH,以下做个笔记,交流交流,顺便求大神指点。

SSH的结合是由Spring作为中介,struts2负责整个网页的MVC框架,Spring负责对象的管理以及事务的支持,Hibernate负责底层持久层的管理其实就是管理数据库。

我开发的时候使用的版本是:struts2 2.3.16.3、Hibernate 4.3.6 Final、Spring 4.1.2 、 Tomcat 8 、 JDK-8

1.首先想项目中导入所有相关的包,为了方便,我把我项目用到的包打包做了个上传

链接: http://pan.baidu.com/s/1dD8bSbB 密码: 4s2r

2.在web.xml里面加入以下代码,大概的意思是,由struts2框架来处理服务器接受到的所有请求,以及让spring来负责对象的生成(我们以前都是通过new来创建对象的,现在可以让spring来帮我们完成这个工作)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns
="http://java.sun.com/xml/ns/javaee"
  xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  id
="WebAp  p_ID"
  version
="2.5"> 3 4 5 <!-- 让struts2 拦截所有的请求 --> 6 <filter> 7 <filter-name>struts2</filter-name> 8 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 9 </filter> 10 <filter-mapping> 11 <filter-name>struts2</filter-name> 12 <url-pattern>/*</url-pattern> 13 </filter-mapping> 14 15 <!-- 这里是让spring框架来负责对象的生成 --> 16 <listener> 17 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 18 </listener> 19 20 </web-app>

顺便科普以下:

filter:拦截器,可以用来拦截特定的请求,/*代表所有的请求,可以用来做权限认证等

listener:监听器,可以监听用户的请求、服务端的操作等。

 

在WEB-INF里面新建一个applicationContext.xml这是用来对spring和Hibernate进行配置的文件。在新版的Spring中允许使用java类来配置,这是后话了。

/WebContent/WEB-INF/applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:tx="http://www.springframework.org/schema/tx"
 5 
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         
10         http://www.springframework.org/schema/tx
11         http://www.springframework.org/schema/tx/spring-tx.xsd
12         
13         http://www.springframework.org/schema/aop
14         http://www.springframework.org/schema/aop/spring-aop.xsd
15         
16         http://www.springframework.org/schema/context
17         http://www.springframework.org/schema/context/spring-context.xsd     
18         ">
19         
20 </beans>

这是一个空的applicationContext.xml里面没有任何的配置

 

Ok!如此一来,你的项目就融入了SSH框架了,下一遍讲解使用Annotation来进行Action配置。

 

posted @ 2014-11-18 11:33  cirno_9  阅读(185)  评论(0)    收藏  举报