read-Atleap-4-标志库的使用

王朝java/jsp·作者佚名  2006-01-31
窄屏简体版  字體: |||超大  

● 标签库的使用

Ø 标记类定义

public class ErrorsTag extends TagSupport {

protected transient final Log log = LogFactory.getLog(ErrorsTag.class);

protected String property;

public String getProperty() {

return property;

}

public void setProperty(String property) {

this.property = property;

}

public int doStartTag() throws JspException {

//在标记类中可以通过pageContext访问到web应用的其他属性和变量

ActionMessages errors = (ActionMessages) pageContext.getAttribute(WebappConstants.ERROR_KEY, PageContext.REQUEST_SCOPE);

pageContext.setAttribute(WebappConstants.ERROR_KEY, errors, PageContext.REQUEST_SCOPE);

pageContext.removeAttribute(WebappConstants.ERROR_KEY, PageContext.SESSION_SCOPE);

}

}

… …

return SKIP_BODY;

}

public int doEndTag() throws JspTagException {

clearAttributes();

return EVAL_PAGE;

}

public void release() {

super.release();

clearAttributes();

}

public void clearAttributes() {

property = null;

}

}

Ø dudu.tld中的标记声明

<taglib>

<tlib-version>1.0</tlib-version>

<jsp-version>2.0</jsp-version>

<short-name>atleap</short-name>

<description><![CDATA[Custom tag library for this application]]></description>

<tag>

<name>errors</name>

<tag-class>com.blandware.atleap.webapp.taglib.core.html.ErrorsTag</tag-class>

<body-content>empty</body-content>

<attribute>

<name>property</name>

<required>false</required>

<rtexprvalue>true</rtexprvalue>

<type>java.lang.String</type>

<description><![CDATA[The server name to use]]></description>

</attribute>

</tag>

</taglib>

Øweb.xml中的定义(可选项)

<taglib>

<taglib-uri>http://dudu.org/taglib</taglib-uri>

<taglib-location>/WEB-INF/dudu.tld</taglib-location>

</taglib>

Ø taglibs.jsp中的定义

<%@ taglib uri="http://atleap.blandware.com/taglib" prefix="dudu" %>

Ø需要页面添加

<%@ include file="/WEB-INF/pages/common/taglibs.jsp" %>

Ø页面调用

<dudu:errors />

● 错误消息处理标签

注:自定义标记封装struts Errors

i) 判断PageContext.REQUEST_SCOPE中是否存在所需的自定义错误

ii) 判断PageContext.SESSION_SCOPE中是否存在所需的自定义错误

iii) 如果不存在错误,什么也不做,返回,如果存在错误,将错误对象转换为

ActionMessages存入PageContext.REQUEST_SCOPE

iv) 使用Struts的TagUtils工具处理错误信息

v) 处理完后,由tagUtils.write(pageContext, results.toString())写如pageContent。

示例类:

atleap\src\web\com\blandware\atleap\webapp\taglib\core\html\ErrorsTag.java

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航