如何使欢迎文件列表与 Spring Servlet 一起使用? [英] How can I make welcome-file-list work with Spring Servlet?

查看:21
本文介绍了如何使欢迎文件列表与 Spring Servlet 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

public class Bootstrap implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {

        container.getServletRegistration("default").addMapping("/resources/*");

        AnnotationConfigWebApplicationContext servletContext =
                new AnnotationConfigWebApplicationContext();
        servletContext.register(ServletContextConfiguration.class);

        ServletRegistration.Dynamic dispatcher =
                container.addServlet("springDispatcher", new DispatcherServlet(servletContext));

        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

继续前进:

@Configuration
@EnableWebMvc
@ComponentScan(
        basePackages = "biz.tugay.booksspringone.controller",
        useDefaultFilters = false,
        includeFilters = @ComponentScan.Filter(Controller.class)
)
public class ServletContextConfiguration {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

}

和 web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <welcome-file-list>
        <welcome-file>/welcome</welcome-file>
    </welcome-file-list>

</web-app>

和我的控制器:

@Controller
public class HelloController {
    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public ModelAndView welcome() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("welcome");
        return modelAndView;
    }
}

当我部署我的应用程序并导航到 localhost:8080/时,我希望 HelloController.welcome 被调用,但事实并非如此.

When I deploy my app and navigate to localhost:8080/ I expect HelloController.welcome to be called, but it is not.

该方法仅在我明确访问http://localhost:8080/welcome

The method is called only if I explicitly visit http://localhost:8080/welcome

我该如何解决这个问题?

How can I fix this?

推荐答案

欢迎文件列表将为您在标签中指定的非请求"文件找到.

A welcome file list will find for that file "not a request" you specified in the tag.

所以

<welcome-file-list>
       <welcome-file>/welcome</welcome-file>
</welcome-file-list>

将根据您在视图解析器中指定的条件查找文件,因此它将找到以下文件

will look for the file with criteria you specified in view resolver, so it will going to find following file

/WEB-INF/views/welcome.jsp

创建该文件,您将被重定向到主页 url 的welcome.jsp.

Create that file and you will be redirected to welcome.jsp for the home url.

这篇关于如何使欢迎文件列表与 Spring Servlet 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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