Taglib principle and implementation Chapter 5: Connecting EL Expression and JSTL Tags

xiaoxiao2021-03-06  71

Chapter 5: Reproducing EL Expression and JSTL Tags

1. Question: You want to work together with JSTL. For example, after processing some logic with your own label, let JSTL processes the rest of the work.

2. See this JSP example: .... <% string name = "Diego"; Request.SetAttribute ("name", name);%>

...

Many JSTL tags support EL expressions, so as long as you put the value into the Request in your own label, other JSTL tags can use them

3. Below this example, get an object from the Request, find the value of its properties, plug it to the 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;}}

Write TLD set diegoyun.setvartag EMPTY value true true property false False var false FALSE Write 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"%> <% man man = new man (); man.setname ("Diego "); Request.setttribute (" man ", man);%> get value from request and set it's profment value inTo request:
Now use outtag of jstl taglib to get the name:
Value IS:

Run, the effect is as follows:

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

4. Conclusion. And JSTL interaction is very useful technology. Many labels that complete basic functions are available in JSTL, such as output, cyclic, conditional selection, etc. Only implement your own labels only when handling your own specific logic, and provides JSTL interaction, which can greatly improve reuse and reduce workload.

转载请注明原文地址:https://www.9cbs.com/read-93308.html

New Post(0)