【发布时间】:2016-07-25 17:10:58
【问题描述】:
这是我的项目:
WebConfig.java:
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter{
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
//strings to views
//success from a controller -* prefix/success/suffix
//success from a controller -* /WEB-INF/pages/success.jsp
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
return resolver;
}
}
AppInitializer.java:
public class WebAppInitializer implements WebApplicationInitializer{
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(WebConfig.class);
container.addListener(new ContextLoaderListener(rootContext));
DispatcherServlet dispatcherServlet = new DispatcherServlet(rootContext);
ServletRegistration.Dynamic registration = container.addServlet("dispatcherServlet", dispatcherServlet);
registration.setLoadOnStartup(1);
registration.addMapping("/");
}
}
PS:我的“index.jsp”页面位于:/WEB-INF/pages/
我仍然没有得到索引页:它说它没有找到。 感谢您的帮助。
【问题讨论】:
-
为什么你认为应该获取索引页?您认为这里的应用程序配置的哪一部分服务于它?你为什么这么认为?
-
好吧,我按照教程做了,这个人确实得到了他的页面,我想知道为什么是我!而且我已经将默认的“/”映射到“索引”,所以我在这里看不到问题..
-
你能链接到教程吗?你认为你的
internalResourceViewResolver()方法是用来做什么的? -
link。而且我认为该方法应该搜索并指向视图,不?
标签: java spring jsp spring-mvc web