java web.xml 中的欢迎文件,spring 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10237416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
welcome-file in web.xml with spring not working?
提问by John Farrelly
I have setup my spring-mvc servlet to match *.pagerequests. I have setup the welcome-file-listin web.xml to be index.page
我已经设置了我的 spring-mvc servlet 来匹配 * .page请求。我已将web.xml 中的欢迎文件列表设置为 index.page
This works when I go to the root of my webserver:
当我转到我的网络服务器的根目录时,这有效:
http://me.comdoes get redirected to http://me.com/index.pagecorrectly.
http://me.com确实被正确重定向到http://me.com/index.page。
However, it doesn't redirect when I use subdirectoris:
但是,当我使用子目录时它不会重定向:
http://me.com/dashboarddoes notget redirected to http://me.com/dashboard/index.page
http://me.com/dashboard并没有重定向到http://me.com/dashboard/index.page
Is there any way to get this mapping working?
有没有办法让这个映射工作?
My web.xml file (extract):
我的 web.xml 文件(摘录):
<welcome-file-list>
<welcome-file>index.page</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>*.page</url-pattern>
</servlet-mapping>
My webdefault.xml (from jetty):
我的 webdefault.xml(来自码头):
<init-param>
<param-name>dirAllowed</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>welcomeServlets</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>redirectWelcome</param-name>
<param-value>false</param-value>
</init-param>
采纳答案by Bozho
It will work only for real, physical directories, not won't work for arbitrary servlet mappings simulating directory structure.
它仅适用于真实的物理目录,不适用于模拟目录结构的任意 servlet 映射。
Spring MVC allows very complex URL mappings, so you'd better handle this scenario with @RequestMapping
Spring MVC 允许非常复杂的 URL 映射,所以你最好用 @RequestMapping
回答by BalusC
The <welcome-file>
should represent a physically existing file in an arbitrary folder which you would like to serve whenever the enduser requests a folder (such as the root /
, but it can also be any other folder such as /foo/
). You only need to understand that the servletcontainer will test its physical existence before performing a forward, if it does not exist then a HTTP 404 page not found error will be returned.
本<welcome-file>
应代表你想成为每当终端用户请求一个文件夹中的任意文件夹实际存在的文件(如根/
,但它也可以是任何其他文件夹,例如/foo/
)。您只需要了解 servletcontainer 将在执行转发之前测试其物理存在,如果它不存在,则会返回 HTTP 404 页面未找到错误。
In your particular case, you do not have a physical index.page
file in your root folder. You have actuallya index.jsp
file in your root folder. The index.page
is merely a virtual URL. So the servletcontainer won't be able to find the physical index.page
file and hence error out with a 404.
在您的特定情况下,您index.page
的根文件夹中没有物理文件。您的根文件夹中实际上有一个index.jsp
文件。这index.page
只是一个虚拟 URL。因此 servletcontainer 将无法找到物理index.page
文件,因此会出现 404 错误。
You can workaround this by fooling the servletcontainer by placing a physically existing index.page
file next to the index.jsp
file in the same folder. That file can just be kept completely empty. The servletcontainer will find the file and then forward to index.page
which will then invoke the controller servlet which in turn will actually serve the index.jsp
as view. That'll work just fine.
您可以通过在同一文件夹中的index.page
文件旁边放置一个物理存在的文件来欺骗 servletcontainer 来解决此问题index.jsp
。该文件可以完全保持为空。servletcontainer 将找到该文件,然后转发到index.page
该文件,然后调用控制器 servlet,后者实际上将实际为index.jsp
as 视图提供服务。那会工作得很好。
回答by Moesio
To avoid forwarding welcome file itself, its better add a mapping for it.
为了避免转发欢迎文件本身,最好为其添加映射。
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>index.html</url-pattern>
</servlet-mapping>
And in case of java configuration you can override two methods in class that extends WebMvcConfigurerAdapter
在 java 配置的情况下,您可以覆盖扩展 WebMvcConfigurerAdapter 的类中的两个方法
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/index");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
If you wanna serve index.html explicitly, turn it into a resource override a method in the same class as below:
如果您想显式地提供 index.html,请将其转换为资源覆盖同一类中的方法,如下所示:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/index.html").addResourceLocations("/WEB-INF/views/index.html");
}
Of course addResourceLocations
must follows the folder choosen to hold your views.
当然addResourceLocations
必须遵循所选的文件夹来保存您的意见。
See these samples
查看这些示例
回答by Ryan Fernandes
This is something you need to probably set in your web server, and so, maybe server specific
这是您可能需要在 Web 服务器中设置的内容,因此,可能是特定于服务器的
For Apache HTTP Server you can achieve this by setting the DirectoryIndex directive like so:
DirectoryIndex index.page
对于 Apache HTTP Server,您可以通过设置 DirectoryIndex 指令来实现这一点,如下所示:
DirectoryIndex index.page
Apparently, someone has already asked this question, and has accepted an answer at web.xml default file in directory (for jetty, or tomcat)?- See if it works for you.
显然,有人已经问过这个问题,并且已经在目录中的web.xml 默认文件中接受了答案(对于 jetty 或 tomcat)?- 看看它是否适合你。