Dict.CN 在線(xiàn)詞典, 英語(yǔ)學(xué)習(xí), 在線(xiàn)翻譯

          都市淘沙者

          荔枝FM Everyone can be host

          統(tǒng)計(jì)

          留言簿(23)

          積分與排名

          優(yōu)秀學(xué)習(xí)網(wǎng)站

          友情連接

          閱讀排行榜

          評(píng)論排行榜

          Taglib原理和實(shí)現(xiàn):再論El和JSTL標(biāo)簽

            問(wèn)題:你想和JSTL共同工作。比如,在用自己的標(biāo)簽處理一些邏輯之后,讓JSTL處理余下的工作。
            
            看這個(gè)JSP例子:
            
            <%
            String name="diego";
            request.setAttribute("name",name);
            %>
            <c:out value="${name}"/>
            ......
            
            許多JSTL標(biāo)簽支持El表達(dá)式,所以,只要你在自己的標(biāo)簽內(nèi)部把值塞進(jìn)request,其他jstl標(biāo)簽就能使用它們
            
            下面這個(gè)例子,從request里面取得對(duì)象,找到它屬性的值,塞到request里去。
            
            package diegoyun;
            
            import javax.servlet.jsp.JspException;
            import javax.servlet.jsp.tagext.TagSupport;
            import org.apache.commons.beanutils.PropertyUtils;
            import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
            
            public class SetVarTag extends TagSupport
            {
            private Object value = null;
            private String property = null;
            private String var = null;
            public void setVar(String var)
            {
            this.var = var;
            }
            public void setProperty(String property)
            {
            this.property = property;
            }
            public void setValue(Object value)throws JspException{
            this.value = ExpressionEvaluatorManager.evaluate( "value", value.toString(), Object.class, this, pageContext);
            }
            public int doEndTag() throws JspException{
            Object propertyValue = null;
            try{
            propertyValue = PropertyUtils.getProperty(value, property);
            }
            catch (Exception e) {
            throw new JspException(e);
            }
            pageContext.setAttribute(var,propertyValue);
            return EVAL_PAGE;
            }
            }
            
            編寫(xiě)TLD
            
            <!--SetVarTag-->
            <tag>
            <name>set</name>
            <tag-class>diegoyun.SetVarTag</tag-class>
            <body-content>empty</body-content>
            <attribute>
            <name>value</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            </attribute>
            <attribute>
            <name>property</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
            </attribute>
            <attribute>
            <name>var</name>
            <required>false</required>
            <rtexprvalue>false</rtexprvalue>
            </attribute>
            </tag>
            
            編寫(xiě)JSP
            
            <%@ page language="java" %>
            <%@ page import="diegoyun.vo.*"%>
            <%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%>
            <%@ taglib uri="/WEB-INF/tlds/c.tld" prefix="c"%>
            <html>
            <body bgcolor="#FFFFFF">
            <%
            Man man = new Man();
            man.setName("diego");
            request.setAttribute("man",man);
            %>
            Get value from request and set it's property value into request:<br>
            <diego:set value="${man}" property="name" var="myname"/>
            now use OutTag of jstl taglib to get the name:<br>
            value is : <c:out value="${myname}" />
            
            </body>
            </html>
            
            運(yùn)行,效果如下:
            
            Get value from request and set it's property value into request:
            now use OutTag of jstl taglib to get the name:
            value is : diego
            結(jié)束語(yǔ)
            和JSTL交互是非常有用的技術(shù)。在JSTL里提供了許多完成基本功能的標(biāo)簽,如輸出,循環(huán),條件選擇等。僅在處理自己特定邏輯的時(shí)候才實(shí)現(xiàn)自己的標(biāo)簽,并提供和jstl交互,能大大提高重用性和減少工作量。

          posted on 2006-10-08 14:29 都市淘沙者 閱讀(865) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): JSP/PHP

          主站蜘蛛池模板: 衡阳市| 婺源县| 黄石市| 肃宁县| 东阳市| 祁连县| 内黄县| 施甸县| 安丘市| 沾化县| 桂阳县| 木里| 任丘市| 云林县| 清流县| 和顺县| 江门市| 赣州市| 铜鼓县| 鄂托克前旗| 山阴县| 北京市| 蓬溪县| 霞浦县| 阜新市| 宕昌县| 山东省| 葵青区| 西乌| 晋宁县| 通州市| 昌黎县| 汝南县| 襄垣县| 上饶市| 会同县| 赫章县| 视频| 托里县| 巩义市| 南澳县|