2: Let TAG support EL expression
1. First look at this example <<% @ Page ContentType = "text / html; charset = GB2312" Language = "java"%> <% @ Taglib URI = "/ Web-INF / TLDS / C.TLD" prefix = "c"%> "Body > <% String tut = "tutorial"; Request.setttribute ("TUT", TUT);%> the string in request is:
body> html>
2. How to support the EL expression Under the path org.hang.taglibs.standard.lang.support, there is a method called ExpressionEvaluatorManager.eValuate. When the EL expression is used as income parameters, call this method, can be automatically Transform EL expression. For example, you want Tag's Value field to support EL expression, then just call in the set method: public void setValue (Object value) throws jspexception {this.Value = expressionEvaluatorManager.eValuate ("Value", value.tostring () , Object.class, this, PageContext);} ExpressionEvaluatorManager.eValuate has four parameters. The first representation of the name of the TAG, is used when the EL expression is wrong. Generally, the name of the attribute is the same. Second requires a string, usually simply call the toString method for the input object. The third is class, usually with Object.class. The fourth use this, the fifth is the PageContext variable. Never think about this method too much. Simply change the name of the property, others can move. Note: When your Tag property supports EL expression, you must declare it as an Object object. As described above, it should be declared as: private object value = null; 3. Instance: Let OutputTag support EL expression
Package diegoyun;
Import javax.servlet.jsp.jspexception; import javax.servlet.jsp.jspwriter; import javax.servlet.jsp.tagext.tagsupport ;. TAGEXT.TAGSUPPORT
Import org.apache.taglibs.standard.lang.support.expressionEvaluatorManager;
public class NewOutputTag extends TagSupport {private Object name = null; public void setName (Object name) throws JspException {this.name = ExpressionEvaluatorManager.evaluate ( "name", name.toString (), Object.class, this, pageContext);} public int doStartTag () throws JspException {try {JspWriter out = pageContext.getOut (); out.print ( "Hello!" name);} catch (Exception e) {throw new JspException (e);} return EVAL_PAGE;} } Add a statement in Diego.Tld
Write JSP test <% @ page language = "java"%> <% @ taglib URI = "/ web-inf / TLDS / DIEGO.TLD" prefix = "DIEGO"%>
<% String s = "diego"; Request.SetaTRibute ("name", s);%> test el supported tag:body> html>
You can see the page output: Test El supported Tag: Hello! Diego