springMVC+hibernate.doc
文本预览下载声明
SpringMVC的搭建
一直以来接触到的都是SSH的框架,形成了MVC模式,本来并没有想着去弄另一个MVC模式,但现在springMVC模式很热,所以我也学习一下,首先我声明一下,这个框架我也是在网上找了一些资料才完成的,源文件等也是利用的网上的现成的,但是有对其进行修改。下面来详细的说一说这个模式的搭建。首先在spring中是以controller来作为控制器(相当于SSH中的action),其他的和SSH框架没有区别。因为Spring是基于注解的,所以在整个的模式中都是采用注解的方式来处理,这个项目是用springMVC+hibernate一起来搭建的。这个项目的搭建我花了很久的时间,也弄了蛮久才成功,希望日后能更加完善!理解更加的深入。
一:整体框架的结构图以及所需的jar包。这里spring是3.0.1,hibernate是用的3.6,数据库是用的mysql 5.6 ,前提工作是要建立好一个数据库,我这里是名为springmvc的数据库来进行操作,这里是采用的hibernate自动更新的方式,所以可以不需要建表只需要建立起数据库就好。
项目框架的代码结构:
二:开始搭建环境。
首先把上面所需的包添加进来后,我们要在/WEB-INF目录下的web.xml里面添加spring的监听器,以及相关的配置。源码如下:
?xml version=1.0 encoding=UTF-8?
web-app version=2.5
xmlns=/xml/ns/javaee
xmlns:xsi=/2001/XMLSchema-instance
xsi:schemaLocation=/xml/ns/javaee
/xml/ns/javaee/web-app_2_5.xsd
display-names3h3/display-name
context-param
param-namecontextConfigLocation/param-name
param-valueclasspath:applicationContext*.xml/param-value
/context-param
listener
listener-classorg.springframework.web.context.ContextLoaderListener/listener-class
/listener
servlet
servlet-namespring/servlet-name
servlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class
init-param
param-namecontextConfigLocation/param-name
param-value/WEB-INF/spring-servlet.xml/param-value
/init-param
load-on-startup1/load-on-startup
/servlet
servlet-mapping
servlet-namespring/servlet-name !-- 这里在配成spring,下边也要写一个名为spring-servlet.xml的文件,主要用来配置它的controller --
url-pattern*.do/url-pattern
/servlet-mapping
welcome-file-list
welcome-fileindex.html/welcome-file
welcome-fileindex.htm/welcome-file
welcome-fileindex.jsp/welcome-file
welcome-filedefault.html/welcome-file
welcome-filedefault.htm/welcome-file
welcome-filedefault.jsp/welcome-file
/welcome-file-list
/web-app
接下来可以编写spring的配置文件,来整合hibernate,主要的配置写在一个专门存放配置文件的源码目录下config文件夹下,这里的applicationContext.xml是spring的主要配置
显示全部