【问题标题】:NPE in putAttribute when invoking tiles调用瓦片时 putAttribute 中的 NPE
【发布时间】:2014-03-08 17:48:48
【问题描述】:

我正在尝试通过瓦片集成来学习 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>

tiles.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(输入验证尚未合并,我也检查了此操作返回成功)。

日志

消息:

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)

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

【问题讨论】:

  • 我通过几处更改解决了这个问题。我犯了 n 个错误。

标签: apache tomcat struts2 tiles


【解决方案1】:

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 用作其他视图技术中切片定义的标记。
  3. 此外,斜线/ 被包含在切片定义名称之前,struts.xml 也会相应调整。

【讨论】:

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