
最近在看spring in action第3版,这里搭建一个简单的spring mvc,也算书没白看,当然老鸟可以不屑了,这里只是给自己做个笔记,配置也尽量删烦就简,
SpringMVC的核心是DispatcherServlet,这个Servlet充当前端控制器。与其他Servlet一样,必须在web应用程序的文件中进行配置。
1. 从web.xml开始
1.web.xml:
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
">
<!--contextConfigLocation参数指定一个spring配置文件路径列表,路径是相对于应用程序根目录的。-->
<!-- 需要告诉ContextLoaderListener需要加载哪些配置文件。如果没有指定,会依照约定上下文加载器会查找/WEB-INF/applicationContext.xml这个Spring配置文件。-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:xml/springContext.xml
</param-value>
</context-param>
<!--ContextLoaderListener是一个Servlet器,除了DispatcherServlet创建的应用上下文以外,它能够加载其他的配置文件到spring应用上下文中。-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--DispatcherServlet会根据一个XML文件来加载其spring应用上下文,而这个文件的名字基于它的<servlet-name>属性来确定,这里为springmvc-servlet-->
<!--关于<load-on-startup>解释:
贴一段英文原汁原味的解释如下:
Servletspecification:
Theload-on-startupelementindicatesthatthisservletshouldbeloaded
(instantiatedandhaveitsinit()called)onthestartupofthewebapplication.
Theoptionalcontentsoftheseelementmustbeanintegerindicatingtheorderinwhich
theservletshouldbeloaded.Ifthevalueisanegativeinteger,ortheelementisnotpresent,
thecontainerisfreetoloadtheservletwheneveritchooses.Ifthevalueisapositiveinteger
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-55464-1.html
我要疯了