java - 使用带有tiles插件的struts2时出现NoSuchDefinitionException。之前的所有问题都已检查

标签 java jsp struts2 apache-tiles

我尝试将tiles与struts 2一起使用并得到NoSuchDefinitionException。 stackoverflow 或 google 上的解决方案都没有解决我的问题,所以我决定发布一个新问题。 下面是struts和tiles的配置。我使用的示例测试页不完整。

a) Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ezviewocean</display-name>
  <description>EZ-View Ocean</description>
        <context-param>
            <param-name> org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
            <param-value>/WEB-INF/tiles.xml</param-value>
        </context-param>
    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>
    <filter>
        <filter-name>struts-prepare</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts-prepare</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

b)struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true"/>
    <constant name="struts.custom.i18n.resources" value="ApplicationResources"/>

    <package name="default" extends="struts-default" namespace="/">
        <result-types>
         <result-type name="tiles" 
         class="org.apache.struts2.views.tiles.TilesResult" />
        </result-types>
        <action name="login" method="execute"
            class="com.schenker.ocean.actions.LoginAction">
            <result name="success" type="tiles">/baseLayout</result>
            <result name="input">/login.jsp</result>
            <result name="fail">/login.jsp</result>
        </action>
    </package>
</struts>

b.5)tiles.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE tiles-definitions PUBLIC
   "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
   "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>

   <definition name="baseLayout" template="/baseLayout.jsp">
      <put-attribute name="title"  value="Template"/>
      <put-attribute name="header" value="/header.jsp"/>
      <put-attribute name="leftmenu"   value="/leftmenu.jsp"/>
      <put-attribute name="body"   value="/body.jsp"/>
      <put-attribute name="footer"   value="/footer.jsp"/>
   </definition>

   <definition name="view" extends="baseLayout">
      <put-attribute name="title"  value="View"/>
      <put-attribute name="body"   value="/viewBody.jsp"/>      
   </definition>

   <definition name="assign" extends="baseLayout">
      <put-attribute name="title"  value="Assign"/>
      <put-attribute name="body"   value="/assignBody.jsp"/>      
   </definition>

</tiles-definitions>

c)baseLayout.jsp

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insertAttribute name="title" ignore="true" />
</title>
</head>

<body>
   <tiles:insertAttribute name="header" /><br/>
   <hr/>
   <tiles:insertAttribute name="leftMenu" /><br/>
   <hr/>
   <tiles:insertAttribute name="body" /><br/>
   <hr/>
   <tiles:insertAttribute name="footer" /><br/>
</body>
</html>

下面是堆栈跟踪。

Stacktraces

org.apache.tiles.definition.NoSuchDefinitionException: /baseLayout.tiles
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:625)
    org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:321)
    org.apache.struts2.views.tiles.TilesResult.doExecute(TilesResult.java:105)
    org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
    com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:371)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:275)
    org.apache.struts2.interceptor.DeprecationInterceptor.intercept(DeprecationInterceptor.java:41)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)

我尝试调试应用程序并检查发送到 TilesResult.class 的值,并发现它与 struts.xml 中定义的完全相同。为什么它不从tiles.xml 中获取该配置?

最佳答案

如果您在结果中使用 .tiles,那么您必须为 Tiles 定义一个配置文件,将每个条目映射到 JSP。

在您的代码中:

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

为了使其正常工作,您需要添加一个tiles.xml 文件,其中包含以下内容:

<tiles-definitions>
  <definition name="somelayout" template="/WEB-INF/jsp/baseLayout.jsp" />
</tiles-definitions>

或者,不使用 Tiles,直接使用 JSP,方法是将结果定义 XML 中的 .tiles 替换为 .jsp。

您可能还需要在 web.xml 中显式加载 Tiles:

 <servlet>
    <servlet-name>tiles</servlet-name>
    <servlet-class>org.apache.tiles.web.startup.TilesServlet</servlet-class>
    <init-param>
        <param-name>org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG</param-name>
        <param-value>
            /WEB-INF/tiles.xml,
        </param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

确保正确处理图 block 请求。

关于java - 使用带有tiles插件的struts2时出现NoSuchDefinitionException。之前的所有问题都已检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26127355/

相关文章:

java - 从 csv 打印格式化表格

java - 使用 GridBagLayout 表示动态仪表板的布局

java - java 中 import 和 class.forName 的区别

java - Spring MVC - 获取错误的消息包

java - 如何使用struts1.2限制浏览器中任何链接的url直接访问

java - 数学公式的问题 - Java

java - Spring 发现 beans 从版本 4.3 更改为版本 5

java - 无法使用 request.getParameter() 或 request.getAttributes() 从同一个 Servlet 中检索数据,尝试都只获取 null

java - 在 Struts2 中显示标签以使用链接

java - struts2中如何使用表单提交复合对象