【问题标题】:How to specify welcome file list in servlet web module 3.0如何在 servlet web 模块 3.0 中指定欢迎文件列表
【发布时间】:2015-10-28 12:11:20
【问题描述】:

我正在使用 web 模块 3.0 创建 servlet,我看到没有与之一起创建的 web.xml,比如说如果我的项目中有多个 jsp 页面,我该如何指定欢迎文件,我们是否有任何注释提到欢迎文件?

【问题讨论】:

  • 什么是web module 3.0

标签: java servlets


【解决方案1】:

是的,您可以有一个 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 工具
  • 选择 “生成部署描述符存根”

【讨论】:

    【解决方案2】:

    您可以结合使用@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>
    

    只需手动创建即可。

    【讨论】:

      猜你喜欢
      • 2011-08-03
      • 2015-09-21
      • 2013-11-28
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      相关资源
      最近更新 更多