【问题标题】:Troubles with apache tiles 2 and JSPapache tile 2 和 JSP 的问题
【发布时间】:2011-05-06 10:52:06
【问题描述】:

我正在尝试让 Tile 2 与 JSP 一起使用。我得到一个空指针异常。我很难找到解释如何进行设置的好文档。我有一个非常简单的用例。我有一个带有名为“内容”的属性的模板。然后,我尝试通过将 jsp 插入“内容”属性来使用模板。我不确定是否需要在 web.xml 文件中设置任何内容?我已经粘贴了我的模板和试图使用该模板的 jsp。

这是模板:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="template" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

        <link rel="stylesheet" href="${pageContext.request.contextPath}/css/jquery-ui-1.8.5.custom.css" type="text/css" />
        <link rel="stylesheet" href="${pageContext.request.contextPath}/css/app.css" type="text/css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js" type="text/javascript"></script>
        <title>Cheetah Home</title>
    </head>
    <body>
        <div id="wrapper">
            <jsp:include page="${pageContext.request.contextPath}/jsp/layout/top.jsp"></jsp:include>
            <template:insertAttribute name="content"></template:insertAttribute>
        </div>
    </body>
</html>

这是一个尝试使用模板的页面:

<%@taglib uri="http://tiles.apache.org/tags-tiles" prefix="template" %>

<template:insertTemplate template="/templates/homeTemplate.jsp">
    <template:putAttribute name="content" value="test.jsp">
    </template:putAttribute>
</template:insertTemplate>

我正在使用 Maven 构建应用程序,并指定了以下依赖项:

    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-servlet</artifactId>
        <version>2.2.2</version>
    </dependency>
     <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-template</artifactId>
        <version>2.2.2</version>
    </dependency>
     <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>2.2.2</version>
    </dependency>

一切正常,但是当我运行应用程序时,我得到:

javax.servlet.ServletException: com.sun.jersey.api.container.ContainerException: org.apache.jasper.JasperException: java.lang.NullPointerException

有人对如何使其工作有任何想法吗?

谢谢!

【问题讨论】:

    标签: java jsp servlets jersey tiles2


    【解决方案1】:

    您必须进行一些配置才能使 Tiles 2 正常工作。文档 (http://tiles.apache.org/tutorial/configuration.html) 说您可以配置 web.xml 以包含启动 servlet、侦听器或过滤器。我尝试了监听器,但它对我不起作用。但是,过滤器做到了。

    <filter>
        <filter-name>Tiles Filter</filter-name>
        <filter-class>org.apache.tiles.web.startup.TilesFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>Tiles Filter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
    

    我遇到的另一个问题是您需要指定 SLF4J 实现,例如 Log4J。还要注意,当您为切片添加 maven 依赖项时,它将为您获取 SLF4J api。您需要确保您在 maven 中指定的实现与磁贴作为依赖项添加的 API 版本相匹配,否则会遇到一些有趣的错误。

    【讨论】:

      【解决方案2】:

      我得到了同样的错误,但它只能通过指定 SLF4J 实现来解决。 Servlet 适合我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-13
        • 1970-01-01
        • 2012-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多