1. First look at this example
<% @ Page contenttype = "text / html; charset = GB2312" Language = "java"%>
<% @ Taglib URI = "/ Web-INF / TLDS / C.TLD" prefix = "c"%>
<%
String tut = "tutorial";
Request.setttribute ("TUT", TUT);
%>
The String in Request IS:
Body>
Html>
2. How to support EL expressions
Under the path org.apache.taglibs.standard.lang.support, there is a method called ExpressionEvaluatorManager.eValuate. When the EL expression is used as incubation, the method is called, and the EL expression can be automatically converted to the TAG. 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 mentioned above, it should be declared as:
Private Object value = null;
3. Example: Let OutputTAG support EL expressions
Package diegoyun;
Import javax.servlet.jsp.jspexception;
Import javax.servlet.jsp.jspwriter;
Import javax.servlet.jsp.tageXt.tagsupport;
Import org.apache.taglibs.standard.lang.support.expressionEvaluatorManager;
Public class newputtag 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 desartTartTag () throws jspexception {
Try
{
Jspwriter out = pageContext.getut ();
Out.print ("Hello!" Name;
}
Catch (Exception E)
{
Throw new JSPEXCEPTION (E);
}
Return Eval_page;
}
}
Add a statement in Diego.TLD
Attribute>
Tag>
Write JSP test
<% @ Page language = "java"%>
<% @ Taglib Uri = "/ Web-INF / TLDS / DIEGO.TLD" prefix = "DIEGO"%>
<%
String s = "DIEGO";
Request.setttribute ("Name", S);
%>
Test El Supported Tag:
Body>
Html>
You can see that the page output is:
Test El Supported Tag:
Hello! Diego