如何使欢迎文件列表与 Spring Servlet 一起工作?
How can I make welcome-file-list work with 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 被调用,但它没有被调用。
仅当我显式访问 http://localhost:8080/welcome
时才会调用该方法
我该如何解决这个问题?
欢迎文件列表会找到您在标签中指定的文件 "not a request"。
所以
<welcome-file-list>
<welcome-file>/welcome</welcome-file>
</welcome-file-list>
将根据您在视图解析器中指定的条件查找文件,因此它将查找以下文件
/WEB-INF/views/welcome.jsp
创建该文件,您将被重定向到 welcome.jsp 主页 url。
这是我的代码:
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 被调用,但它没有被调用。
仅当我显式访问 http://localhost:8080/welcome
时才会调用该方法我该如何解决这个问题?
欢迎文件列表会找到您在标签中指定的文件 "not a request"。
所以
<welcome-file-list>
<welcome-file>/welcome</welcome-file>
</welcome-file-list>
将根据您在视图解析器中指定的条件查找文件,因此它将查找以下文件
/WEB-INF/views/welcome.jsp
创建该文件,您将被重定向到 welcome.jsp 主页 url。