使用struts 2创建tile时获取NoSuchAttributeException

时间:2016-08-25 22:17:53

标签: java struts2 attributes tiles

我收到org.apache.tiles.jsp.taglib.NoSuchAttributeException:找不到属性'banner'。运行tile项目时出现异常。下面是代码。我从互联网上下载了一个项目并运行它。它工作正常,我发现两个项目都没有任何差异。不过,下面的代码抛出异常。请指教。

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="/home.jsp">
        <put-attribute name="title" value="Receipe Maker"/>
        <put-attribute name="banner" value="/banner.jsp"/>
        <put-attribute name="menu" value="/menu.jsp"/>
        <put-attribute name="body" value="/about.jsp" />
        <put-attribute name="footer" value="/footer.jsp"/>
    </definition>

    <definition name="teaLayout" extends="baseLayout">
        <put-attribute name="title" value="Tea Maker" />
        <put-attribute name="body" value="/tea.jsp" />
    </definition>

    <definition name="coffeeLayout" extends="baseLayout">
        <put-attribute name="title" value="Coffee Maker" />
        <put-attribute name="body" value="/coffee.jsp" />
    </definition>

    <definition name="milkShakeLayout" extends="baseLayout">
        <put-attribute name="title" value="Milk Shake Maker" />
        <put-attribute name="body" value="/milk-shake.jsp" />
    </definition>

</tiles-definitions>

的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>Archetype Created Web Application</display-name>

    <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>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>home.jsp</welcome-file>
    </welcome-file-list>

</web-app>

struts.xml中

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <package name="default" extends="struts-default">
        <result-types>
            <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
        </result-types>
        <action name="*Menu" method="{1}" class="com.practice.tiles.MenuAction">
            <result name="home" type="tiles">home</result>
            <result name="tea" type="tiles">tea</result>
            <result name="coffee" type="tiles">coffee</result>
            <result name="milkShake" type="tiles">milk-shake</result>
        </action>
    </package>
</struts>

MenuAction.java

package com.practice.tiles;

import com.opensymphony.xwork2.ActionSupport;

public class MenuAction extends ActionSupport {

    /** */
    private static final long serialVersionUID = -7471860198571879105L;

    public String home() {
        return "home";
    }

    public String tea() {
        return "tea";
    }

    public String coffee() {
        return "coffee";
    }

    public String milkShake() {
        return "milk-shake";
    }

}

Maven Dependencies

Project Structure

1 个答案:

答案 0 :(得分:0)

我必须进行以下更改才能解决问题:

首先,我创建了baseLayout并且不直接使用它。现在tiles.xml看起来像下面的代码:

<tiles-definitions>
<definition name="baseLayout" template="/baseLayout.jsp">
    <put-attribute name="title" value="Receipe Maker"/>
    <put-attribute name="banner" value="/banner.jsp"/>
    <put-attribute name="menu" value="/menu.jsp"/>
    <put-attribute name="body" value="/about.jsp" />
    <put-attribute name="footer" value="/footer.jsp"/>
</definition>

<definition name="home" extends="baseLayout">
    <put-attribute name="title" value="Home"/>
    <put-attribute name="body" value="/home.jsp" />
</definition>

<definition name="tea" extends="baseLayout">
    <put-attribute name="title" value="Tea Maker" />
    <put-attribute name="body" value="/tea.jsp" />
</definition>

<definition name="coffee" extends="baseLayout">
    <put-attribute name="title" value="Coffee Maker" />
    <put-attribute name="body" value="/coffee.jsp" />
</definition>

<definition name="milkShake" extends="baseLayout">
    <put-attribute name="title" value="Milk Shake Maker" />
    <put-attribute name="body" value="/milk-shake.jsp" />
</definition>

</tiles-definitions>

其次,确保struts.xml中的结果值必须与tiles.xml中定义元素的属性值匹配: 例如,在struts.xml中,结果标记为:<result name="teaValue" type="tiles">tea</result>

执行的action方法返回teaValue,而tea是tile定义的名称。

那就是它,现在它正在发挥作用。