Spring MVC 程序首页的设置
作者:轻舞肥羊 日期:2014-03-31
在用 Spring mvc 写程序,配置 DispatcherServlet 拦截规则为拦截所有( <url-pattern>/</url-pattern> )时,应用程序首页的定义很有可能出问题,在项目中一般来说有如下几种解决办法.
1. 在 webcontent 下定义一个首页. index.jsp 或者 index.html ,然后跳转到controller 处理.
这种情况下,通常会像如下这样处理
程序代码
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
而在index.jsp 里面通常会这么写:
程序代码
<head>
<meta http-equiv="Refresh" content="0; URL=/mycontroller/test">
</head>
这样,就推跳转到 mycontroller/test 去处理。
方法 2:交给 spring mvc controller 去处理,不要配置默认首页
首先,不要配置首页, 必须设置成如下:
程序代码
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
这样,web 服务器就知道,不需要他来处理,而是由应用程序自己来处理。这个时候,spring mvc 的 servlet 配置就起作用了,可以如下配置:
程序代码
<mvc:view-controller path="/" view-name="redirect:/mycontroller/test" />
这种配置方式下,效果与前面的一样,都是类似于 redirect 做到的。
1. 在 webcontent 下定义一个首页. index.jsp 或者 index.html ,然后跳转到controller 处理.
这种情况下,通常会像如下这样处理

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
而在index.jsp 里面通常会这么写:

<head>
<meta http-equiv="Refresh" content="0; URL=/mycontroller/test">
</head>
这样,就推跳转到 mycontroller/test 去处理。
方法 2:交给 spring mvc controller 去处理,不要配置默认首页
首先,不要配置首页, 必须设置成如下:

<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
这样,web 服务器就知道,不需要他来处理,而是由应用程序自己来处理。这个时候,spring mvc 的 servlet 配置就起作用了,可以如下配置:

<mvc:view-controller path="/" view-name="redirect:/mycontroller/test" />
这种配置方式下,效果与前面的一样,都是类似于 redirect 做到的。
除非申明,文章均为一号门原创,转载请注明本文地址,谢谢!
评论: 0 | 引用: 0 | 查看次数: -
发表评论