在Apache Tiles 2中,importAttribute和useAttribute之间的区别是什么?

时间:2012-12-11 03:09:10

标签: jsp apache-tiles

据我所知,两个标签都将tile上下文中的变量导入JSP,因此变量在JSP中可见。请详细说明importAttribute和useAttribute之间的区别。

1 个答案:

答案 0 :(得分:3)

显然两者之间没有区别,只是在useAttribute中允许您指定期望变量的类名。

以下是importAttribute代码的代码:

@Override
public void doTag() throws JspException {
    JspContext jspContext = getJspContext();
    Map<String, Object> attributes = model.getImportedAttributes(JspUtil
            .getCurrentContainer(jspContext), name, toName, ignore,
            jspContext);
    int scopeId = JspUtil.getScope(scopeName);
    for (Map.Entry<String, Object> entry : attributes.entrySet()) {
        jspContext.setAttribute(entry.getKey(), entry.getValue(), scopeId);
    }
}

以及useAttribute代码的代码:

@Override
public void doTag() throws JspException {
    JspContext pageContext = getJspContext();
    Map<String, Object> attributes = model.getImportedAttributes(JspUtil
            .getCurrentContainer(pageContext), name, id, ignore,
            pageContext);
    if (!attributes.isEmpty()) {
        int scopeId = JspUtil.getScope(scopeName);
        pageContext.setAttribute(getScriptingVariable(), attributes
                .values().iterator().next(), scopeId);
    }
}