Dispatcher servlet返回unrenderd JSP(Java Config)

时间:2015-10-05 11:31:17

标签: java spring jsp servlets spring-boot

我正在使用Spring Boot环境来运行Web应用程序,使用带有嵌入式Apache Tomcat / 7.0.54的Servlet 3规范。当我运行应用程序时,我得到了未呈现的JSP,如下所示:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<%@ page session="false" %>
<html>
  <head>
   <title>Spitter</title>
    <link rel="stylesheet" 
      type="text/css" 
      href="<c:url value="/resources/style.css" />" >
  </head>
  <body>
    <h1><s:message code="spitter.welcome" text="Welcome" /></h1>
    <s:url value="/spitter/register" var="registerUrl" />
    <a href="<s:url value="/spittles" />">Spittles</a> | 
    <a href="${registerUrl}">Register</a>
  </body>
</html>

我使用基于Java的配置来配置应用程序:

public class SpitterWebInitializer extends      AbstractAnnotationConfigDispatcherServletInitializer {

 @Override
 protected Class<?>[] getRootConfigClasses() {
   return new Class<?>[] { RootConfig.class };
 }

 @Override
 protected Class<?>[] getServletConfigClasses() {
  return new Class<?>[] { WebConfig.class };
 }

 @Override
 protected String[] getServletMappings() {
   return new String[] { "/" };
 }

}

Web Config

@Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig extends WebMvcConfigurerAdapter {

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

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

   @Override
   public void addResourceHandlers(ResourceHandlerRegistry registry) {
     // TODO Auto-generated method stub
     super.addResourceHandlers(registry);
   }

 }
}

控制器类:

@Controller
@RequestMapping("/")
public class HomeController {

  @RequestMapping(method = GET)
  public String home(Model model) {
    return "home";
  }

}

请告诉我需要配置的内容。

2 个答案:

答案 0 :(得分:0)

将servlet映射设置为/ *

public class SpitterWebInitializer extends      AbstractAnnotationConfigDispatcherServletInitializer {

 @Override
 protected Class<?>[] getRootConfigClasses() {
   return new Class<?>[] { RootConfig.class };
 }

 @Override
 protected Class<?>[] getServletConfigClasses() {
  return new Class<?>[] { WebConfig.class };
 }

 @Override
 protected String[] getServletMappings() {
   return new String[] { "/*" };
 }

}

答案 1 :(得分:0)

问题在于嵌入式tomcat,我使用嵌入式Apache-tomcat- 7.0.54 作为Spring Boot的一部分,我遇到了这个问题。当我在外部Apache-tomcat-7.0.52上运行应用程序时,它运行正常,所以不确定Apache-tomcat中是否存在错误 - 7.0.54