Tiles 3不是Rendering List属性

时间:2013-01-26 18:33:05

标签: jsp struts2 apache-tiles struts-tags tiles-3

在尝试将列表属性添加到定义之前,我对tile 3.0.1没有任何问题。没有错误,定义也能正确呈现,除了列表属性似乎没有出现在JSP上。

我正在使用CompleteAutoloadListener加载切片,这里是web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <listener>
        <listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</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>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param> 
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 
    <welcome-file-list>
        <welcome-file>/index.action</welcome-file>
    </welcome-file-list>
</web-app>

以下是定义的外观(tiles-defs.xml):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
<tiles-definitions>
    <definition name="head-default" template="/WEB-INF/template/head.jsp">
        <put-list-attribute name="items">
            <add-attribute value="/style/cssbase-min.css" />
            <add-attribute value="/style/cssfonts-min.css" />
            <add-attribute value="/style/cssreset-min.css" />
            <add-attribute value="/style/grids-min.css" />
            <add-attribute value="/style/style.css" />
        </put-list-attribute>   
    </definition>

    <definition name="default" template="/WEB-INF/template/template.jsp">
        <put-list-attribute name="items" inherit="true"/>
        <put-attribute name="head" value="head-default"/>
        <put-attribute name="header" value="/WEB-INF/template/header.jsp"/>
        <put-attribute name="body" value="/WEB-INF/template/body.jsp"/>
        <put-attribute name="footer" value="/WEB-INF/template/footer.jsp"/>
    </definition>
    <definition name="REGEXP:\/recruiter#candidate-input\.(.*)"  extends="default">
        <put-attribute name="head" value="/WEB-INF/template/recruiter/head.jsp"/>
        <put-attribute name="body" value="/WEB-INF/content/recruiter/candidate-input.jsp"/>
    </definition>
    <definition name="REGEXP:(.*)#(.*)"  extends="default">
        <put-list-attribute name="items" inherit="true"/>
        <put-attribute name="body" value="/WEB-INF/content{1}/{2}"/>
    </definition>
</tiles-definitions>

最后这里是一个tile(head.jsp),我试图输出“items”的值,实际上我放弃了,只是想看看迭代是否会起作用(列表中应该有5个项目) ,所以只想打印五个字符串,但不输入循环。

<%@taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <s:iterator value="items">
        iterator,
    </s:iterator>
    <c:forEach var="item" items="${items}">
        ${item}
    </c:forEach>
    <script src="<s:url value='/script/jquery/1.8.1/jquery.min.js'/>"></script>
    <script src="<s:url value='/script/jquery.sort.js'/>"></script>
    <title>A Title</title>
</head>

有什么想法吗?

更新:最终工作标题磁贴,包括使用struts标记。

<%@taglib prefix="tiles" uri="http://tiles.apache.org/tags-tiles"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <tiles:importAttribute name="items"/>
    <s:iterator value="#attr.items">
        <s:property/>
    </s:iterator>
    <script src="<s:url value='/script/jquery/1.8.1/jquery.min.js'/>"></script>
    <script src="<s:url value='/script/jquery.sort.js'/>"></script>
    <title>A Title</title>
</head>

因为tiles将导入的属性“items”放入页面范围,所以只能使用#attr范围从struts2访问。

1 个答案:

答案 0 :(得分:11)

在Tiles 2.2中,代码an example from Tiles 2.2 documentation中缺少一行:

<tiles:useAttribute id="list" name="items" classname="java.util.List" />

但是在tiles 3.0.1中,现在使用了importAttribute标记:

<tiles:importAttribute name="items"/>

然后是一个有效的解决方案:

<tiles:importAttribute name="items"/>
<c:forEach var="item" items="${items}">
    ${item}
</c:forEach>