每当学习一门新技术时候,大部分人直接就是百度Bing搜索,借鉴别人的博客例子等。我也是一样,但是我经常想,这些牛人的例子如何写出来的,如何深入扩展其它功能等等。所以,我会做多一步,就是挖掘官方文档。
本文例子的软件环境:
IntelliJ IDEA 15 CE (社区版)
Maven3.0 (IDE捆绑)
JDK1.8(IDE捆绑)
本文目标:
本文不是解决什么高深问题,而是教大家查阅官方文档,以Spring MVC 基本页面跳转,整合第三方开源框架为例子。
适读人群:
理解Servelt运行原理,会用Maven,对Spring和mybatis框架有大致了解
相信大部分人构建应用的时候,是直接从别人写好的代码项目开始的,或者直接用付费的IDE生成通用的应用模版。但是,试想一下,假如让你从”零”开始构建一个web应用,IDE功能只能帮你生成最简单的web folder结构,你手上只有Spring的官方文档。那你如何配置servlet / servelet-mapping / Spring DispatcherServlet 等web.xml里最基本的标签? 如何配置Spring ApplicationContex.xml等等?
例如,IDE生成简单的Web.xml如下:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>
如果你是有Spring使用经验的话,会记得要用servlet标签等,可是你记不起完整代码,这时候试试打开“http://java.sun.com/dtd/web-app_2_3.dtd“就会发现:
<!--
The servlet-class element contains the fully qualified class name
of the servlet.
Used in: servlet
-->
<!ELEMENT servlet-class (#PCDATA)>
<!--
The servlet-mapping element defines a mapping between a servlet
and a url pattern
Used in: web-app
-->
<!ELEMENT servlet-mapping (servlet-name, url-pattern)>
其实不用死记的,那个dtd文件有标签的定义说明,还可以看看有哪些标签是平时不常见但可能用得着的吧,此处略举例。如果想了解更多,可以查看Oracle公司关于Web.xml的配置说明网址:
https://docs.oracle.com/cd/E28280_01/web.1111/e13712/web_xml.htm#WBAPP502
扯远了,回归Spring MVC如何配置来。
第一步:利用IntelliJ 生成Maven的Web项目
第二步:根据官网配置项目POM文件:
Spring Framework Source:
http://projects.spring.io/spring-framework/
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
</dependencies>
第三步:参考Spring MVC文档配置web.xml(Part VI. The Web):
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-introduction
<web-app>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/example/*</url-pattern>
</servlet-mapping>
</web-app>
至此,假如运行上面配置,会出现如下错误:
问题一:
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
原因:
未配置相应的Artifacts,那么如何找到相应的Maven Artefacts呢
解决:
其实Table 2.1. Spring Framework Artefacts有相应说明
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#overview-web
凭经验尝试加入spring-webmvc,问题解决 。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
继续运行,会出现:
问题二:
BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/example-servlet.xml];
原因:
文档中21.2的章节有说明如下
Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.
解决:
参考文档Figure 21.3图下面的配置说明–>This can be configured by setting an empty contextConfigLocation servlet init parameter, as shown below:
尝试在前面的< servlet >基础上增加 < init-param > 配置contextConfigLocation
...
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>...</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
...
</servlet>
问题解决。服务器启动成功。
下篇文章会接着教如何设置Spring MVC的controller
本文旨在通过实例演示如何利用官方文档构建SpringMVC Web应用,从基本配置到解决问题的详细步骤,帮助开发者深入了解SpringMVC的底层实现,并学会灵活运用官方文档解决实际开发中遇到的问题。
503





