java 如何在 servlet 3.0 的 web.xml-less 中定义 <welcome-file-list> 和 <error-page>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13450044/
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
How to define <welcome-file-list> and <error-page> in servlet 3.0's web.xml-less?
提问by Wins
I have existing web-app which I want to convert into web.xml-less of servlet's 3.0. I've managed to make it working, however there are 2 tags in web.xml which I still don't know the equivalent code in web.xml-less environment.
我有现有的 web 应用程序,我想将其转换为 servlet 3.0 的 web.xml-less。我已经设法让它工作,但是在 web.xml 中有 2 个标签,我仍然不知道 web.xml-less 环境中的等效代码。
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/pageNotFound</location>
</error-page>
Any help is appreciated
任何帮助表示赞赏
回答by Piotr Nowicki
In Servlets 3.0 you don't need a web.xml for many cases, however, sometimes it's required or just useful. Your case is just one of them - there is no special annotations to define welcome-file list or error-pages.
在 Servlets 3.0 中,在许多情况下您不需要 web.xml,但是,有时它是必需的或只是有用的。您的情况只是其中之一 - 没有特殊的注释来定义欢迎文件列表或错误页面。
Another thing is - would you really like to have them hardcoded? There are some valid use-cases for annotation / programmatic based configuration and for declarative configuration in XML. Moving to Servlets 3.0 doesn't necessarily means getting rid of web.xml at all cost.
另一件事是 - 你真的想让它们硬编码吗?对于基于注释/程序化的配置和 XML 中的声明性配置,有一些有效的用例。迁移到 Servlets 3.0 并不一定意味着不惜一切代价摆脱 web.xml。
I would find the entries you posted a better example of configuration in XML. Firstly - they can be changed from deployment to deployment and secondly - they affect whole application and not any particular Servlet.
我会发现您在 XML 中发布了更好的配置示例的条目。首先 - 它们可以从部署更改为部署,其次 - 它们影响整个应用程序而不是任何特定的 Servlet。
回答by Александр Андреев
For analog welcome-page-list put this in
对于模拟欢迎页面列表,将其放入
@EnableWebMvc
@Configuration
@ComponentScan("com.springapp.mvc")
public class MvcConfig extends WebMvcConfigurerAdapter {
...
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/*.html").addResourceLocations("/WEB-INF/pages/");
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
...
}
回答by Mrityunjay
In Spring Boot or general Spring MVC app for following scenario:
在 Spring Boot 或一般 Spring MVC 应用程序中用于以下场景:
Static files can be served from locations registered with a custom ResourceHandlerRegistry. We have a static resource index.htmland it can accessed at localhost:8080/index.html. We want to just redirect localhost:8080/request to localhost:8080/index.html, following code will can be used.
可以从使用自定义 ResourceHandlerRegistry 注册的位置提供静态文件。我们有一个静态资源index.html,它可以在localhost:8080/index.html访问。我们只想将localhost:8080/请求重定向到localhost:8080/index.html,可以使用以下代码。
package in.geekmj.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" };
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(CLASSPATH_RESOURCE_LOCATIONS);
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addRedirectViewController("/", "/index.html");
}
}
Now accessing localhost:8080/will redirect to localhost:8080/index.html
现在访问localhost:8080/将重定向到localhost:8080/index.html