Spring MVC - 找不到页面

时间:2016-07-25 17:10:59

标签: java spring jsp spring-mvc web

所以这是我的项目:

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

}

P.S:我的" index.jsp"页面位于:/ WEB-INF / pages /

我还没有得到索引页面:它说没找到它。 感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

尝试将@Bean注释添加到该解析器,并将@ComponentScan(basePackages =" yourPackages")添加到配置类

尝试改变解析器:

@Bean
public ViewResolver viewResolver()
{
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");

    return viewResolver;
}