欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Taglib 原理和實(shí)現之嵌套和屬性讀取 JSP 關(guān)鍵詞:Taglib,原理,實(shí)現之嵌套,...
Taglib 原理和實(shí)現之嵌套和屬性讀取
此文章由教程中國(http://www.upschool.com.cn)提供 如果您覺(jué)得可以,請推薦給同好.本站將在近期正式對外開(kāi)放教程貼吧論壇,將設有專(zhuān)家欄,如果您有問(wèn)題,可隨時(shí)得到解決.
 1、問(wèn)題:在request里有一個(gè) Man 對象,它有兩個(gè)屬性:name和age?,F在,我們想用一個(gè)嵌套的tag,父tag取得對象,子tag取得name屬性并顯示在頁(yè)面上。例如,它的形式如下:

<diegwith object="${Man}">
<diegoutput property="name"/>
</diegwith>
  object 支持el表達式,表示取得 Man 對象。output的property表示從該對象取得名為name的屬性。

  2、如何支持tag之間的嵌套

  在子tag里調用getParent 方法,可以得到父tag對象。用 findAncestorWithClass 方法,則可以通過(guò)遞歸找到想要找的tag。例如

<diegwith object="${people}"> <!--表示取得一個(gè)對象-->
<diegwithCollection property="men"> <!--表示取得對象里的一個(gè)屬性,這個(gè)屬性是個(gè) Collection,Collection里添加了許多man,每個(gè)man有名字和年齡-->
<diegoutput property="name"/> <!--取得name屬性并顯示-->
</diegwithCollection>
</diegwith>
  對于最內層的outputTag來(lái)說(shuō),調用getParent,可以得到 withCollectionTag,通過(guò)如findAncestorWithClass(this,WithTag.class)的方式,可以得到withTag。得到Tag之后,就可以取得Tag的屬性,進(jìn)行業(yè)務(wù)邏輯處理,然后輸出到j(luò )sp

  3、如何支持類(lèi)屬性查找功能

  顯然,在上面的outputTag中,我們要根據屬性的名字,查找類(lèi)中有沒(méi)有這個(gè)屬性。然后取出屬性的值并顯示。通常,這可以編寫(xiě)自己的反射函數來(lái)完成。更簡(jiǎn)單的辦法,是通過(guò) BeanUtil 的PropertyUtils方法來(lái)完成功能。BeanUtil 是apache上的一個(gè)開(kāi)源項目。

  示例如下:

import org.apache.commons.beanutils.PropertyUtils;
......
property = PropertyUtils.getProperty(currentClass, propertyName);
  propertyName是待查找屬性的名字,例如上面的"name",currentClass是待查找的類(lèi),例如上面的People

  記得把 commons-beanutils.jar添加到WEB-INF\lib目錄下

  4、現在讓我們實(shí)現開(kāi)篇提出的問(wèn)題,編寫(xiě)WithTag如下:

package diegoyun;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

/**
* @author chenys
*/
public class WithTag extends BodyTagSupport
{
 private Object value = null;
 private Object output = null;

 public void setOutput(Object output)
 {
  this.output = output;
 }
 public Object getValue()
 {
  return value;
 }
 public void setValue(Object value)throws JspException
 {
  this.value = ExpressionEvaluatorManager.evaluate("value", value.toString(), Object.class, this, pageContext);
 }
 public int doStartTag()
 {
  return EVAL_BODY_INCLUDE;
 }
 public int doEndTag()throws JspException
 {
  try
  {
   pageContext.getOut().print(output);
  }
  catch (IOException e)
  {
   throw new JspException(e);
  }
  return EVAL_PAGE;
 }
}
  編寫(xiě) NestedOutputTag 如下:

package diegoyun;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.beanutils.PropertyUtils;

/**
* @author chenys
*/
public class NestedOutputTag extends BodyTagSupport
{
 private String property = null;

 public void setProperty(String property)
 {
  this.property = property;
 }

 public int doEndTag()throws JspException
 {
  WithTag parent =(WithTag)getParent();
  if(parent==null)
   throw new JspException("Can not find parent Tag ");
   try
   {
    Object propertyValue = PropertyUtils.getProperty(parent.getValue(), property);
    parent.setOutput(propertyValue);
   }
   catch (Exception e)
   {
    throw new JspException(e);
   }
   return EVAL_PAGE;
  }
}
  在包diegoyun下添加一個(gè)包vo,在vo下寫(xiě)一個(gè)Man類(lèi):

package diegoyun.vo;

/**
* @author chenys
*/
public class Man
{
 private String name = null;
 private int age = 0;

 public int getAge()
 {
  return age;
 }
 public void setAge(int age)
 {
  this.age = age;
 }
 public String getName()
 {
  return name;
 }
 public void setName(String name)
 {
  this.name = name;
 }
}
  寫(xiě)TLD

<!--WithTag-->
<tag>
<name>with</name>
<tag-class>diegoyun.WithTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!--OutputTag3-->
<tag>
<name>nestedout</name>
<tag-class>diegoyun.NestedOutputTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>property</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>

  寫(xiě)JSP頁(yè)面

<%@ page language="java" %>
<%@ page import="diegoyun.vo.*"%>
<%@ taglib uri="/WEB-INF/tlds/diego.tld" prefix="diego"%>

<html>
<body bgcolor="#FFFFFF">
<%
Man man = new Man();
man.setName("diego");

request.setAttribute("man",man);
%>
Test nested tag:
<br>
<diegwith value="${man}">
<diegnestedout property="name"/>
</diegwith>
</body>
</html>
  運行頁(yè)面,則可以看到:

Test nested tag:
diego
  5、結束語(yǔ):

  上述例子簡(jiǎn)單描繪了嵌套的Tag之間如何交互。通常子Tag負責取得數據,然后設置父Tag的屬性,最后在父Tag里顯示到j(luò )sp頁(yè)面。如上面的例子,父 Tag 的 output 表示待打印的對象,通過(guò) nestedoutTag 取得name的值,設置output,然后打印出來(lái)。

  通過(guò)支持El表達式和動(dòng)態(tài)屬性聯(lián)結,Tag可以實(shí)現強大的處理功能。將邏輯都集中到Tag里,極大的簡(jiǎn)化頁(yè)面的編寫(xiě)。
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
JSP頁(yè)面標簽的編碼實(shí)現(JSP)
[教程]自定義JSP中的Taglib標簽之一簡(jiǎn)單入門(mén)篇(無(wú)參數的自定義標簽)
自定義標簽(TagSupport )
[教程]自定義JSP中的Taglib標簽之五自定義標簽之SimpleTagSupport使用
jsp自定義標簽一
自定義JSP標簽
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久