简单配置的方式,直接展示静态网页,不经过Controller。
web.xml 中什么没有配置任何有关欢迎页的信息!其实这时等效于如下配置:这个会由Web容器最先访问!
//-未指定欢迎页时,缺省等于如下配置。这个应该不同的Web服务器可以设置,但大多数都如此-。
index.html
index.htm
index.jsp
在web.xml中什么都不写的时候,默认访问WebContent目录下的index页面,不管是index.html还是index.jsp页面。但是这个页面不经过Controller的处理,相当于静态的页面。
此外也可以通过如下的方式,指定首页要显示的视图页面:
/WEB-INF/jsp/hello.jsp
以上方式,在浏览器中输入http://localhost:8080/工程名/,即可返回指定的页面。
2.在静态页面上进行跳转。核心 代码:
项目目录下,有个index.html文件,加入如下内容进行跳转:
跳转到的url全路径就相当于 http://localhost:8080/项目名/spring/。这个路径就会由mvc 的DispatcherServlet来处理。为什么呢,是因为web.xml中进行了如下url配置:
appServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/spring/app/servlet-context.xml
1
appServlet
/spring/*
此时在浏览器输入http://localhost:8080/项目名时候,浏览器的地址会自动的跳转到 http://localhost:8080/项目名/spring/,根据此规则,就会对应为MVC 路径路由中的 /。也就是HomeController。
@Controller
public class HomeController
{
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model)
{
return "home";
}
}
然后找到对应的home.jsp。
整个流程如此。其实缺省为什么设置这么一个spring路径。主要目的是为了提升spring对url的处理效率。spring/下的分支都交由spring来处理,其它的就可以交由web 服务器。
如果一切都交给spring处理,我们就要将 / 进行拦截。嗯,一定会由很多静态资源、或者其它动态jsp是另有用途,也会先被spring拦截,然后再当做另外来处理。可以是可以,但是效率上就会觉得多了一步。
但是,另一方面,我们在规划url时,可能会尽可能的减短,以方便用户的输入;同时,规划url时,才不会考虑spring的效率呢,也就是url设计先行。这个时候,通常不会有spring这个特定的路径;也就是spring要将就url的规划。也就是要对 / 进行拦截了。
3.配置servlet。
之前的SpingMVC配置控制器的代码:
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
contextConfigLocation
/WEB-INF/applicationContext.xml,
org.springframework.web.context.ContextLoaderListener
/WEB-INF/jsp/hello.jsp
hello
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/hello-servlet.xml
1
hello
*.htm
问题的由来:
welcome-file-list一般情况下只能使用静态网页,如果非要把他配置成SpringMVC的控制器URL就会报错
解决的方法:
仔细看了一些资料,发现welcome-file-list可以转向到servlet,但是!!!前提是servlet不能有扩展名,否则就当成静态文件处理了,那么这样的话就尝试了定义个没有扩展名的SpringMVC控制器URL。修改配置文件如下:
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
contextConfigLocation
/WEB-INF/applicationContext.xml,
org.springframework.web.context.ContextLoaderListener
index
hello
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/hello-servlet.xml
1
hello
/index
hello
*.htm
注意:welcome-file-list配置的是没有 / 的 index,下面为SpringMVC控制器单独注册了一个 /index 的URL(这个有 “/”)
Controller写法:
@Controller
public class Shouye {
@RequestMapping(value = "/index")
public String index(){
System.out.print("333shouye");
return "index";
}
}
4.直接对根路径进行拦截
必须在web.xml中加入如下:
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
contextConfigLocation
/WEB-INF/applicationContext.xml,
org.springframework.web.context.ContextLoaderListener
hello
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/hello-servlet.xml
1
hello
/
hello
*.htm
此时,web服务器就知道,根路径不要web服务器来处理,而是由程序自身来处理。这时,index.html也就不起作用了。
然后,根路径就被 Shouye 这个Controller拦截了;因为其中配置了对”/”的映射。
@Controller
public class Shouye {
@RequestMapping(value = "/")
public String index(){
System.out.print("333shouye");
return "index";
}
}