最近由于工作需要,搭建了一个spring+hibernate的框架平台,用的是纯springMVC的框架,发现收益良多。以前也是一直用spring+struts(或者webwork)的框架作开发,可是真正自己去独自一个人搭建一个平台的机会很有限,都是和别的同事一起来搭建,有很多不清楚的地方,也没有去深入的了解,总是不求甚解。闲话少叙,下面就进入开始工作。
第一步:下载需要的各个java的类包。
主要的就是spring.jar\hibernate3.jar等等,根据各自需要,下载相应的类包,我前台准备用jstl标签,所以,还下载了jstl的包。
第二步,配置web.xml文件。
首先,在web.xml文件中加入spring的配置文件的信息。
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
其中:/WEB-INF/applicationContext.xml是对应的applicationContext.xml,路经信息可以根据需要更改。
然后,加入spring的DispatcherServlet信息和加载spring的标签
<servlet>
<servlet-name>contract</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>contract</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/spring</taglib-uri>
<taglib-location>/WEB-INF/spring.tld</taglib-location>
</taglib>
其中:
<servlet-mapping>
<servlet-name>contract</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
加载web.xml时,程序会自动去寻找contract-servlet.xml文件,所以<servlet-name>contract</servlet-name>中的值,要和对应的servlet文件名字的前半部分一致。
<url-pattern>*.htm</url-pattern> 表示url的后缀名,类似于struts中的do。我尝试了一下,好像是可以自己定义的,只不过好像spring的约定里面大家都用html作为后缀名。
至此,web.xml文件已经完整,如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>eCommerce Identity Record System</display-name>
<description>
eCommerce Identity Record System application
</description>
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>contract</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>contract</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/spring</taglib-uri>
<taglib-location>/WEB-INF/spring.tld</taglib-location>
</taglib>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/jsp/uncaughtException.jsp</location>
</error-page>
<jsp-config>
<jsp-property-group>
<el-ignored>false</el-ignored>
</jsp-property-group>
</jsp-config>
</web-app>