我有现有的web-app,我想将其转换为web.xml-less servlet 3.0。我已经设法使它工作,但是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>
感谢任何帮助
答案 0 :(得分:30)
在Servlets 3.0中,在许多情况下你不需要web.xml,但是,有时它是必需的或者只是有用的。您的案例只是其中之一 - 没有特殊的注释来定义欢迎文件列表或错误页面。
另一件事是 - 你真的想让它们硬编码吗?基于注释/程序的配置和XML中的声明性配置有一些有效的用例。迁移到Servlets 3.0并不一定意味着不惜一切代价摆脱web.xml。
我会在您发布的条目中找到更好的XML配置示例。首先 - 它们可以从部署更改为部署,其次 - 它们会影响整个应用程序,而不会影响任何特定的Servlet。
答案 1 :(得分:10)
对于模拟欢迎页面列表,请将其放入
@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");
}
...
}
答案 2 :(得分:2)
在Spring Boot或一般的Spring MVC应用程序中,用于以下场景:
可以从使用自定义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");
}
}
现在访问 localhost:8080 / 将重定向到 localhost:8080 / index.html
答案 3 :(得分:1)
在 Spring Boot 2.0 中你可以使用这个代码
@Configuration
public class TomcatInitializer implements
WebServerFactoryCustomizer<TomcatServletWebServerFactory> , TomcatContextCustomizer {
@Override
public void customize(TomcatServletWebServerFactory factory) {
factory.addContextCustomizers(this);
}
private ErrorPage createStatusErrorPage(int errorCode, String location) {
ErrorPage errorPage = new ErrorPage();
errorPage.setErrorCode(errorCode);
errorPage.setLocation(location);
return errorPage;
}
private ErrorPage createExceptionErrorPage(Class<?> klass, String location) {
ErrorPage errorPage = new ErrorPage();
errorPage.setExceptionType(klass);
errorPage.setLocation(location);
return errorPage;
}
@Override
public void customize(Context context) {
context.addWelcomeFile("/index.jsp");
context.addErrorPage(createStatusErrorPage(404, "/404.jsp"));
context.addErrorPage(createExeptionErrorPage(Exception.class, "exception.jsp"));
context.setSessionTimeout(120);
}
}