struts 2 tiles NoSuchDefinitionException

房东的猫 提交于 2019-12-13 03:43:21

问题


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


回答1:


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>



回答2:


I encountered this problem today. Although it was a question long ago, I found the solution as below:

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

Just replacing the type tiles with dispatcher works for me.



来源:https://stackoverflow.com/questions/4770344/struts-2-tiles-nosuchdefinitionexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!