现在的位置: 首页 > 综合 > 正文

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

2013年04月05日 ⁄ 综合 ⁄ 共 2303字 ⁄ 字号 评论关闭

    标签库的使用

Ø 标记类定义

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)           使用StrutsTagUtils工具处理错误信息

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

示例类:

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

抱歉!评论已关闭.