I am trying to learn Struts2 f/w with tiles integration but while implementing i am getting NPE in putAttributeTag.

After i click signin i need to move to home page, but i am getting exception while opening home page.

Following is some relevant piece of code.

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 (other divs for other part of templates are also defined but i have not included them here, if they are needed i will update 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 contains simple one line text only. actions.SigninAction returns success only (input validation are not incorporated yet, I have checked also this Action returns success).

Logs

Message:

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

Stacktrace

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)

and

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)

and

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)

It would be nice if you would point out my mistake(s).

有帮助吗?

解决方案

The reason for NPE was that in baseLayout, i was using putAttribute tag instead of insertAttribute.

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

When this putAttribute would have been evaluated, then, value parameter was missing. Hence, resulting in throwing a NPE.

Fix: use insertAttribute tag as we will be fetching the value and inserting it into the baselayout.

Also some other things i changed after going though this blog are as follows:

  1. Name of the Jsp's start with Captial letter.
  2. In tiles definition, .tiles is used as marker for tiles definition from other view technology.
  3. Also, a slash / is incorporated before tiles definition name, and struts.xml is adjusted accordingly.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top