如何在WebApplicationInitializer.onStartup()中指定欢迎文件列表 [英] how to specify welcome-file-list in WebApplicationInitializer.onStartup()

查看:311
本文介绍了如何在WebApplicationInitializer.onStartup()中指定欢迎文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我有一个Web应用程序,我们在其中使用web.xml来配置该应用程序. web.xml具有欢迎文件列表".

Currently I have a web application where we are using web.xml to configure the application. The web.xml has welcome-file-list.

<web-app>  
   ...
   <welcome-file-list>  
     <welcome-file>home.html</welcome-file>  
   </welcome-file-list>  
</web-app>  

我们计划使用spring框架,并使用java类进行应用程序配置.

We are planning to use spring framework and use java class for application configuration.

class MyApplication extends WebApplicationInitializer {
    public void onStartUp(ServletContext context){
        ...
    }
}

如何在此Java类中指定welcome-file-list?

How do I specify welcome-file-list in this java class?

推荐答案

在开发基于纯Java的Configuration的Spring MVC应用程序时,我们可以通过扩展应用程序的配置类扩展WebMvcConfigurerAdapter 类并覆盖

While developing Spring MVC application with pure Java Based Configuration, we can set the home page by making our application configuration class extending the WebMvcConfigurerAdapter class and override the addViewControllers method where we can set the default home page as described below.

@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");
  }

}

它返回home.jsp视图,该视图可用作主页.无需创建自定义控制器逻辑即可返回首页视图.

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.

用于

配置预先配置有 响应状态代码和/或呈现响应主体的视图.这 在不需要自定义控制器逻辑的情况下很有用 -例如呈现主页,执行简单的站点URL重定向,返回包含HTML内容的404状态,不包含内容的204状态等等.

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.

第二种方法-对于静态HTML文件首页,我们可以在配置类中使用以下代码.它将返回index.html作为主页-

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 -

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");
    }

第三种方式-下面的请求映射"/"也将返回home视图,该视图可用作应用程序的主页.但是建议使用以上方法.

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.

@Controller
public class UserController {
    @RequestMapping(value = { "/" })
    public String homePage() {
        return "home";
    }
}

这篇关于如何在WebApplicationInitializer.onStartup()中指定欢迎文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆