SpringMVC使用Java配置从Webcontent文件夹提供JSP文件

时间:2015-02-16 16:04:01

标签: java spring jsp spring-mvc spring-java-config

我正在将Struts2 Web应用程序转换为Spring Web应用程序。在这个过程中,我将应用程序从基于XML的转换为基于Java配置。我能够将我们的RESTful Backend转换为Spring WebServices,它运行良好。但是,应用程序的入口点需要通过服务器提供的jsp。

我无法让Java识别位于WEB-INF文件夹中的index.jsp。我一直收到错误

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers

我已尝试将InternalResourceViewResolver配置为指向WEB-INF文件夹,但这似乎不起作用。

任何人都能看到我错过的或做错了吗?

WebInitializer.java

public class WebInitializer extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
    application.showBanner(false);
    application.sources(AppBaseConf.class);
    return application;
}

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

    super.onStartup(ctx);
}

AppBaseConf.java

@EnableWebMvc
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class AppBaseConf extends WebMvcConfigurerAdapter {

    protected static Logger logger = Logger.getLogger(AppBaseConf.class);

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver(){
        logger.info("Configuring View Resolver");
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/templates/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

PageController.java

@Controller("pageController")
public class PageController {

    @RequestMapping(value= "/home")
    public String showHomePage() {
        return "index";
    }
}

0 个答案:

没有答案