您的位置:首页 > 编程语言 > Java开发

<转载>springmvc(29):spring默认欢迎页设置

2018-04-09 14:41 537 查看
简单配置的方式,直接展示静态网页,不经过Controller。 

web.xml 中什么没有配置任何有关欢迎页的信息!其实这时等效于如下配置:这个会由Web容器最先访问!

//-未指定欢迎页时,缺省等于如下配置。这个应该不同的Web服务器可以设置,但大多数都如此-。
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
1
2
3
4
5

在web.xml中什么都不写的时候,默认访问WebContent目录下的index页面,不管是index.html还是index.jsp页面。但是这个页面不经过Controller的处理,相当于静态的页面。

此外也可以通过如下的方式,指定首页要显示的视图页面:
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/hello.jsp</welcome-file>
</welcome-file-list>
1
2
3

以上方式,在浏览器中输入http://localhost:8080/工程名/,即可返回指定的页面。

2.在静态页面上进行跳转。核心 代码:
<meta http-equiv="Refresh" content="0; URL=spring/">
 

项目目录下,有个index.html文件,加入如下内容进行跳转:
<html>
<head>
<meta http-equiv="Refresh" content="0; URL=spring/">
</head>
</html>
1
2
3
4
5

跳转到的url全路径就相当于 http://localhost:8080/项目名/spring/。这个路径就会由mvc 的DispatcherServlet来处理。为什么呢,是因为web.xml中进行了如下url配置:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/app/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
1
2
3
4
5
6
7
8
9
10
11
12
13
14

此时在浏览器输入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";
}
}
1
2
3
4
5
6
7
8
9

然后找到对应的home.jsp。

整个流程如此。其实缺省为什么设置这么一个spring路径。主要目的是为了提升spring对url的处理效率。spring/下的分支都交由spring来处理,其它的就可以交由web 服务器。

如果一切都交给spring处理,我们就要将 / 进行拦截。嗯,一定会由很多静态资源、或者其它动态jsp是另有用途,也会先被spring拦截,然后再当做另外来处理。可以是可以,但是效率上就会觉得多了一步。

但是,另一方面,我们在规划url时,可能会尽可能的减短,以方便用户的输入;同时,规划url时,才不会考虑spring的效率呢,也就是url设计先行。这个时候,通常不会有spring这个特定的路径;也就是spring要将就url的规划。也就是要对 / 进行拦截了。

3.配置servlet。 

之前的SpingMVC配置控制器的代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">

<!-- 为spring配置监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,
<!-- /WEB-INF/hello-dao.xml,
/WEB-INF/hello-service.xml, -->
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 默认首页定义 -->
<welcome-file-list> <welcome-file>/WEB-INF/jsp/hello.jsp</welcome-file> </welcome-file-list>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 通过初始化参数声明配置文件位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/hello-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

</web-app>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

此时浏览器中输入http://localhost:8080/Hello/即可进入到指定的静态首页。

问题的由来: 

welcome-file-list一般情况下只能使用静态网页,如果非要把他配置成SpringMVC的控制器URL就会报错

解决的方法: 

仔细看了一些资料,发现welcome-file-list可以转向到servlet,但是!!!前提是servlet不能有扩展名,否则就当成静态文件处理了,那么这样的话就尝试了定义个没有扩展名的SpringMVC控制器URL。修改配置文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">

<!-- 为spring配置监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,
<!-- /WEB-INF/hello-dao.xml,
/WEB-INF/hello-service.xml, -->
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 指定welcome-file-list要转向的servlet -->
<welcome-file-list>
<welcome-file>index</welcome-file>  <!-- 注意 -->
</welcome-file-list>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 通过初始化参数声明配置文件位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/hello-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/index</url-pattern>     <!-- 注意 -->
</servlet-mapping>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

</web-app>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

注意:welcome-file-list配置的是没有 / 的 index,下面为SpringMVC控制器单独注册了一个 /index 的URL(这个有 “/”) 

Controller写法:
@Controller
public class Shouye {
@RequestMapping(value = "/index")
public String index(){
System.out.print("333shouye");
return "index";
}

}
1
2
3
4
5
6
7
8
9
10

此时浏览器中输入http://localhost:8080/Hello/,即可进入到Shouye 返回的视图。

4.直接对根路径进行拦截 

必须在web.xml中加入如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">

<!-- 为spring配置监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,
<!-- /WEB-INF/hello-dao.xml,
/WEB-INF/hello-service.xml, -->
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 指定默认页面 -->
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 通过初始化参数声明配置文件位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/hello-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

</web-app>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

此时,web服务器就知道,根路径不要web服务器来处理,而是由程序自身来处理。这时,index.html也就不起作用了。

然后,根路径就被 Shouye 这个Controller拦截了;因为其中配置了对”/”的映射。
@Controller
public class Shouye {
@RequestMapping(value = "/")
public String index(){
System.out.print("333shouye");
return "index";
}

}
1
2
3
4
5
6
7
8
9
10

此时浏览器中输入http://localhost:8080/Hello/,即可进入到Shouye 返回的视图。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: