如何在servlet 3.0的web.xml-less中定义和?

我有现有的web-app,我想将其转换为web.xml-less servlet 3.0。 我已经设法使它工作,但是web.xml中有2个标签,我仍然不知道web.xml-less环境中的等效代码。

 /index.jsp   404 /pageNotFound  

任何帮助表示赞赏

在Servlets 3.0中,对于许多情况,您不需要web.xml,但是,有时它是必需的或只是有用的。 您的案例只是其中之一 – 没有特殊的注释来定义欢迎文件列表或错误页面。

另一件事是 – 你真的想让它们硬编码吗? 基于注释/程序的配置和XML中的声明性配置有一些有效的用例。 迁移到Servlets 3.0并不一定意味着不惜一切代价摆脱web.xml。

我会在你发布的条目中找到一个更好的XML配置示例。 首先 – 它们可以从部署更改为部署,其次 – 它们会影响整个应用程序,而不会影响任何特定的Servlet。

对于模拟欢迎页面列表,请将其放入

 @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"); } ... } 

在Spring Boot或一般Spring MVC应用程序中,用于以下场景:

可以从使用自定义ResourceHandlerRegistry注册的位置提供静态文件。 我们有一个静态资源index.html ,它可以在localhost:8080 / index.html访问。 我们只想将localhost:8080 / request重定向到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"); } } 

现在访问localhost:8080 /将重定向到localhost:8080 / index.html