【发布时间】:2015-10-28 12:11:20
【问题描述】:
我正在使用 web 模块 3.0 创建 servlet,我看到没有与之一起创建的 web.xml,比如说如果我的项目中有多个 jsp 页面,我该如何指定欢迎文件,我们是否有任何注释提到欢迎文件?
【问题讨论】:
-
什么是web module 3.0?
我正在使用 web 模块 3.0 创建 servlet,我看到没有与之一起创建的 web.xml,比如说如果我的项目中有多个 jsp 页面,我该如何指定欢迎文件,我们是否有任何注释提到欢迎文件?
【问题讨论】:
是的,您可以有一个 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).setViewName("forward:/index.html");
}
...
}
或者如果您仍然需要 web.xml 文件,您可以使用以下步骤在 Eclipse 中手动创建它
【讨论】:
您可以结合使用@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>
只需手动创建即可。
【讨论】: