spring 如何在 WebApplicationInitializer.onStartup() 中指定欢迎文件列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30972676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to specify welcome-file-list in WebApplicationInitializer.onStartup()
提问by user2032118
Currently I have a web application where we are using web.xml to configure the application. The web.xml has welcome-file-list.
目前我有一个 Web 应用程序,我们使用 web.xml 来配置该应用程序。web.xml 有欢迎文件列表。
<web-app>
...
<welcome-file-list>
<welcome-file>home.html</welcome-file>
</welcome-file-list>
</web-app>
We are planning to use spring framework and use java class for application configuration.
我们计划使用 spring 框架并使用 java 类进行应用程序配置。
class MyApplication extends WebApplicationInitializer {
public void onStartUp(ServletContext context){
...
}
}
How do I specify welcome-file-list in this java class?
我如何在这个java类中指定welcome-file-list?
回答by Omkar
While developing Spring MVC application with pure Java Based Configuration, we can set the home page by making our application configuration class extending the WebMvcConfigurerAdapterclass and override the addViewControllersmethod where we can set the default home page as described below.
在使用纯 Java Based Configuration 开发 Spring MVC 应用程序时,我们可以通过使我们的应用程序配置类扩展WebMvcConfigurerAdapter类并覆盖addViewControllers方法来设置主页,我们可以如下所述设置默认主页。
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.myapp.controllers" })
public class ApplicationConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver getViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}
}
It returns home.jsp
view which can be served as home page. No need to create a custom controller logic to return the home page view.
它返回home.jsp
可以作为主页的视图。无需创建自定义控制器逻辑来返回主页视图。
The JavaDoc for addViewControllersmethod says -
addViewControllers方法的 JavaDoc说 -
Configure simple automated controllers pre-configured with the response status code and/or a view to render the response body. This is useful in cases where there is no need for custom controller logic -- e.g. render a home page, perform simple site URL redirects, return a 404 status with HTML content, a 204 with no content, and more.
配置简单的自动化控制器,这些控制器预先配置了响应状态代码和/或视图以呈现响应正文。这在不需要自定义控制器逻辑的情况下很有用——例如呈现主页、执行简单的站点 URL 重定向、返回带有 HTML 内容的 404 状态、没有内容的 204 状态等等。
2nd way- For static HTML file home page we can use the code below in our configuration class. It will return index.html
as a home page -
第二种方式- 对于静态 HTML 文件主页,我们可以在我们的配置类中使用以下代码。它将index.html
作为主页返回-
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
3rd way- The request mapping "/" below will also return home
view which can be served as a home page for an app. But the above ways are recommended.
第三种方式- 下面的请求映射“/”也将返回home
可以作为应用程序主页的视图。但建议采用以上方式。
@Controller
public class UserController {
@RequestMapping(value = { "/" })
public String homePage() {
return "home";
}
}
回答by Neeraj Jain
You can't
你不能
As specified in Java Doc
public interface WebApplicationInitializer
Interface
to be implemented in Servlet 3.0+ environments in order to configure the ServletContext programmatically -- as opposed to (or possibly in conjunction with) the traditional web.xml-based approach.
public interface WebApplicationInitializer
Interface
将在 Servlet 3.0+ 环境中实现,以便以编程方式配置 ServletContext —— 与传统的基于 web.xml 的方法相反(或可能结合)。
but you still need minimal configuration in web.xml , such as for
但是您仍然需要在 web.xml 中进行最少的配置,例如 for
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
回答by Kartik Mavani
@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");
}
...
}
This might help.
这可能会有所帮助。