struts 2 tiles NoSuchDefinitionException

Sumit Jain picture Sumit Jain · Jan 22, 2011 · Viewed 11.9k times · Source

I am getting this exception when using struts 2 with tiles

org.apache.tiles.definition.NoSuchDefinitionException: /index.jsp

//tiles.xml

<tiles-definitions>
<definition name="baseLayout" template="/index.jsp">
    <put-attribute name="title" value="/Template" />
    <put-attribute name="header" value="/Header.jsp" />
    <put-attribute name="menu" value="/Menu.jsp" />
    <put-attribute name="body" value="/body.jsp" />
    <put-attribute name="footer" value="/Footer.jsp" />
</definition>
<tiles-definitions>

//index.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<tiles:insertAttribute name="header"/>
<tiles:insertAttribute name="footer"/>   
</body>
</html>

//web.xml

<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
</listener>
<context-param>
<param-name>org.apache.tiles.impl.BasicTilesContainer.  
DEFINITIONS_CONFIG</param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>

//struts.xml

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

    <action name="login" class="com.medics.action.LoginAction">
        <result name="SUCCESS" type="tiles">/index.jsp</result>
    </action>
    </package> 

I have searched a lot but found nothing

Answer

Quaternion picture Quaternion · Jan 23, 2011
org.apache.tiles.definition.NoSuchDefinitionException: /index.jsp

Means that there is no tiles definition, that is no definition of the name "/index.jsp"

When using struts and tiles... Your request comes into struts then out to tiles where tiles composes the view so you shouldn't have any tiles definitions called "anything.jsp".

So just replace

<result name="SUCCESS" type="tiles">/index.jsp</result>

with

<result name="SUCCESS" type="tiles">baseLayout/result>

Now that is resolved... I would rename index.jsp, template.jsp (I think it's less confusing), why do you have a put-attribute called "/Template" ?

Now to fix the issue I recommended that you change the struts2 result target to "baseLayout" but this is probably not what you mean, so you probably want to use your baseLayout definition for new pages so add a new definition:

<definition name="index" extends="baseLayout">
  <put-attribute name="title" value="My Title for Index" />
  <put-attribute name="body" value="/index.jsp" />
</definition>

Now the above will take that value in defaultTemplate and add (or if the name is the same, override) what was in the base template creating a page for index.jsp, and now your struts.xml should have

 <result name="SUCCESS" type="tiles">index</result>