apache - 调用 tiles 时 putAttribute 中的 NPE

标签 apache tomcat struts2 tiles

我正在尝试通过 tiles 集成学习 Struts2 f/w,但在实现时我在 putAttributeTag 中遇到了 NPE。

点击登录后,我需要转到主页,但打开主页时出现异常。

以下是一些相关的代码。

struts.xml

<package name="default" extends="struts-default" namespace="/">
    <result-types>
        <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
        <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"></result-type>
    </result-types>


    <action name="signin" class="actions.SigninAction" method="execute">
        <result name="success" type="tiles">home</result>
    </action>

</package>

瓷砖.xml

<tiles-definitions>
    <definition name="baseLayout" template="/baseLayout.jsp">
        <put-attribute name="title" value=""></put-attribute>
        <put-attribute name="header" value="/header.jsp"></put-attribute>
        <put-attribute name="leftmenu" value="/leftmenu.jsp"></put-attribute>
        <put-attribute name="footer" value="/footer.jsp"></put-attribute>
        <put-attribute name="rightmenu" value="/rightmenu.jsp"></put-attribute>
        <put-attribute name="content" value=""></put-attribute>
    </definition>

    <definition name="home" extends="baseLayout">
        <put-attribute name="content" value="/home.jsp"></put-attribute>
        <put-attribute name="title" value="Home"></put-attribute>
    </definition>
</tiles-definitions>

baselayout(也定义了模板其他部分的其他div,但我没有在这里包含它们,如果需要它们我会更新baselayout)

<title><tiles:insertAttribute name="title" ignore="true"></tiles:insertAttribute>
</title>
</head>
<body>
        <div id="leftmenu" class=".leftmenu">
            <b>Add New</b><br> Update<br> Search<br> Remove
        </div>

        <div id="content" class=".content">
            <tiles:putAttribute name="content"></tiles:putAttribute>
        </div>

home.jsp 仅包含简单的一行文本。 actions.SigninAction 仅返回 success(输入验证尚未合并,我还检查了此 Action 返回成功)。

日志

留言:

ServletException including path '/baseLayout.jsp'.
ServletException including path '/baseLayout.jsp'.

堆栈跟踪

org.apache.tiles.TilesException: ServletException including path '/baseLayout.jsp'.
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:614)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:246)
    org.apache.struts2.views.tiles.TilesResult.doExecute(TilesResult.java:105)

org.apache.tiles.util.TilesIOException: ServletException including path '/baseLayout.jsp'.
    org.apache.tiles.servlet.context.ServletTilesRequestContext.wrapServletException(ServletTilesRequestContext.java:298)
    org.apache.tiles.servlet.context.ServletTilesRequestContext.forward(ServletTilesRequestContext.java:200)
    org.apache.tiles.servlet.context.ServletTilesRequestContext.dispatch(ServletTilesRequestContext.java:179)
    org.apache.tiles.context.TilesRequestContextWrapper.dispatch(TilesRequestContextWrapper.java:72)

java.lang.NullPointerException
    org.apache.tiles.jsp.taglib.PutAttributeTag.execute(PutAttributeTag.java:204)
    org.apache.tiles.jsp.taglib.RoleSecurityTagSupport.doEndTag(RoleSecurityTagSupport.java:75)
    org.apache.jsp.baseLayout_jsp._jspx_meth_tiles_005fputAttribute_005f0(baseLayout_jsp.java:188)
    org.apache.jsp.baseLayout_jsp._jspService(baseLayout_jsp.java:129)

如果你能指出我的错误就太好了。

最佳答案

NPE 的原因是在 baseLayout 中,我使用了 putAttribute 标签而不是 insertAttribute

<div id="content" class=".content">
    <tiles:putAttribute name="content"></tiles:putAttribute>
</div>

当这个 putAttribute 被评估时,value 参数丢失了。因此,导致抛出 NPE。

修复: 使用 insertAttribute 标签,因为我们将获取值并将其插入到 baselayout 中。

还有一些我在经历之后改变的其他事情 this blog如下:

  1. Jsp 的名称以大写字母开头。
  2. 在 tiles 定义中,.tiles 被用作来自其他 View 技术的 tiles 定义的标记。
  3. 另外,在 tile 定义名称之前加入了一个斜杠 /,并且相应地调整了 struts.xml

关于apache - 调用 tiles 时 putAttribute 中的 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22272839/

相关文章:

java - Servlet 和 Applet 工作时通信失败

tomcat - 如何配置 tomcat-users.xml 以保护 tomcat 中的页面?

hibernate - 为什么类元空间在 Java 8 中会随着时间的推移而增加?

java - ftl 模板中的 escapeHtml ="false"引发参数类型不匹配

eclipse - 避免在struts2中Action执行两次

java - 没有为操作和结果定义结果

javascript - 使用 Apache 的 mod_proxy_wstunnel 连接到 PHP,而不使用第 3 方 API

apache - .htaccess - 将所有重定向到根文件夹或子文件夹的 index.php

apache - 在Docker和Mac OS X之间共享文件时Apache上的问题

java - 使用静态页面在 spring mvc 部署中苦苦挣扎