2

As far as I know both tags import variables from tiles context to JSP so variables are made visible in the JSP. Please elaborate the difference between importAttribute and useAttribute.

4

1 回答 1

3

显然,两者之间没有区别,除了 inuseAttribute允许您指定预期变量的类名。

这是标签的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);
    }
}
于 2012-12-11T03:52:27.057 回答