据我所知,两个标签都将tile上下文中的变量导入JSP,因此变量在JSP中可见。请详细说明importAttribute和useAttribute之间的区别。
答案 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);
}
}