Servlet Spec 3.0不呈现JSP包含

时间:2013-12-22 03:30:37

标签: java spring jsp tomcat servlets

我尝试过使用Tomcat 7和Tomcat 8 RC 5,但我的JSP包不会渲染。

我正在使用Spring MVC(不要认为这很重要)。

我的JSP看起来像这样:

<jsp:include page="includes/header.jsp" />
It Worked!
<jsp:include page="includes/footer.jsp" />

当页面呈现时,它看起来像这样

<jsp:include page="includes/header.jsp" />
It Worked!
<jsp:include page="includes/footer.jsp" />

在我的Pom文件中,我包括:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
    </dependency>

我的web.xml文件如下所示:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     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"
     id="Blog" version="3.0">
<display-name>Blog</display-name>
  <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/jsp/error/404.html</location>
</error-page>

My App Initializer看起来像这样:

public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext context = new     AnnotationConfigWebApplicationContext();
    context.setConfigLocation("com.package.to.AppConfig");
    servletContext.addListener(new ContextLoaderListener(context));
    ServletRegistration.Dynamic servlet = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
    servlet.setLoadOnStartup(1);
    servlet.addMapping("/*");
}

我做错了什么?为什么我的包含没有正确呈现?

我也尝试了以下相同的结果:

<%@ include file="includes/header.jsp" %>
It Worked!
<%@ include file="includes/footer.jsp" %> 

控制器方法:

@Controller
public class PageController {

@Autowired
PageService pageService;

@RequestMapping(value = "/*", method = RequestMethod.GET)
public String index(HttpServletRequest request){
    String path = request.getRequestURI().substring(request.getContextPath().length());
    Page page = pageService.getPageByUrl(path);
    if(page == null){
        throw new ResourceNotFoundException();
    }

    return page.getTemplate().getPageTemplatePath();
}

}

的AppConfig

@Configuration
@ComponentScan({"package.to.scan"})
@EnableTransactionManagement
@EnableWebMvc
@PropertySource("/${env:prod}.properties")
@Import({DataSourceConfig.class})
public class AppConfig extends WebMvcConfigurerAdapter {

@Autowired
Environment environment;

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

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

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/styles/**").addResourceLocations("/styles/");
    registry.addResourceHandler("/images/**").addResourceLocations("/images/");
    registry.addResourceHandler("/scripts/**").addResourceLocations("/scripts/");
}

}

1 个答案:

答案 0 :(得分:2)

要快速解决问题,请将DispatcherServlet映射更改为/,而不是/*

您的/*DispatcherServlet映射。换句话说,任何未命名的RequestDispatcher forwardinclude都会经历同一个Servlet

因此,当您的InternalResourceViewResolver已解析的视图尝试转发时,例如

/WEB-INF/jsp/somepage.jsp

将选择DispatcherServlet来处理它。由于您没有处理该网址请求的@Controller方法,因此

注册了SimpleUrlHandlerMapping
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

将被使用。 DefaultServletHandlerConfigurer注册DefaultServletHttpRequestHandler,将请求分发给default Servlet。它通过按名称

获取default Servlet来实现此目的
@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    RequestDispatcher rd = this.servletContext.getNamedDispatcher(this.defaultServletName);
    if (rd == null) {
        throw new IllegalStateException("A RequestDispatcher could not be located for the default servlet '" +
                this.defaultServletName +"'");
    }
    rd.forward(request, response);
}

使用Tomcat,这通常是org.apache.catalina.servlets.DefaultServlet

  

使用的大多数Web应用程序的默认资源服务servlet   提供HTML页面和图像等静态资源。

换句话说,它不会处理jsps,只会将它们直接提供给HttpServletResponse OutputStream

如果您将DispatcherServlet网址映射更改为/ Servlet容器将再次通过匹配网址的规则。这些在Servlet Specification中描述。第三个是

  

如果网址路径中的最后一个段包含扩展名(例如.jsp),   servlet容器将尝试匹配处理的servlet   请求延期。扩展名被定义为   最后一个'。'字符后的最后一段

在这种情况下,Tomcat默认会找到映射到

org.apache.jasper.servlet.JspServlet
<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.jspx</url-pattern>
</servlet-mapping>

此网址映射与/WEB-INF/jsp/somepage.jsp匹配,因此被选中。 JspServlet会渲染jsps,这样就可以了。


有些相关说明,servlet-api应由Servlet容器提供,而不是您的网络应用程序。

更改此

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
</dependency>

到这个

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

无论这是否是原因,您都应该进行此更改。