如何在servlet web模块3.0中指定欢迎文件列表



我正在使用web模块3.0创建servlet,我发现没有同时创建web.xml,比如说,如果我的项目中有多个jsp页面,我如何指定欢迎文件,我们有任何注释来提及欢迎文件吗?

您可以组合使用annotations@WebServlet和web.xml文件。

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
            ...
        </welcome-file-list>             
</web-app>

只需手动创建即可。

是的,您可以有一个Java文件来提及欢迎文件

@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("/").setStatusCode(HttpStatus.MOVED_PERMANENTLY).set‌​ViewName("forward:/index.html");
    }
...
}

或者,如果您仍然想要web.xml文件,您可以使用以下步骤在Eclipse中手动创建它

  • 右键单击您的web项目
  • 选择Java EE工具
  • 选择"生成部署描述符存根"

最新更新