使用基于 Java 的纯配置且没有 web.xml 开发 spring mvc 应用程序时如何设置欢迎文件?
How to set the welcome-file when developing spring mvc application with pure Java based configuration and no web.xml?
我正在使用 Spring MVC 开发一个 Web 应用程序,具有纯 Java 且没有 web.xml 配置。我确实编写了下面的 class 代码来加载 bean 并设置 url 模式。如何设置欢迎文件?
public class MyAppWebAppIntializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext appCtx = new AnnotationConfigWebApplicationContext();
appCtx.register(ApplicationContextConfig.class);
Dynamic dispatcher = servletContext.addServlet(
"SpringDispatcher", new DispatcherServlet(appCtx));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
在使用纯 Java 基于配置开发 Spring MVC 应用程序时,我们可以通过使我们的应用程序配置 class 扩展 WebMvcConfigurerAdapter class and override the 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");
}
}
它returns home.jsp
可以作为主页的视图。无需为 return 主页视图创建自定义控制器逻辑。
JavaaddViewControllers 方法的文档说 -
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文件主页,我们可以在我们的配置中使用下面的代码class -
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
第三种方式-
下面的请求映射“/”也将 return home.jsp 可以用作应用程序的主页。但建议采用上述流程。
@Controller
public class UserController {
@RequestMapping(value = { "/" })
public String homePage() {
return "home";
}
}
我正在使用 Spring MVC 开发一个 Web 应用程序,具有纯 Java 且没有 web.xml 配置。我确实编写了下面的 class 代码来加载 bean 并设置 url 模式。如何设置欢迎文件?
public class MyAppWebAppIntializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext appCtx = new AnnotationConfigWebApplicationContext();
appCtx.register(ApplicationContextConfig.class);
Dynamic dispatcher = servletContext.addServlet(
"SpringDispatcher", new DispatcherServlet(appCtx));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
在使用纯 Java 基于配置开发 Spring MVC 应用程序时,我们可以通过使我们的应用程序配置 class 扩展 WebMvcConfigurerAdapter class and override the 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");
}
}
它returns home.jsp
可以作为主页的视图。无需为 return 主页视图创建自定义控制器逻辑。
JavaaddViewControllers 方法的文档说 -
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文件主页,我们可以在我们的配置中使用下面的代码class -
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
}
第三种方式- 下面的请求映射“/”也将 return home.jsp 可以用作应用程序的主页。但建议采用上述流程。
@Controller
public class UserController {
@RequestMapping(value = { "/" })
public String homePage() {
return "home";
}
}