Question: You want to work with JSTL. For example, after processing some logic with your own tags, let JSTL process the remaining work.
Take a look at the JSP Example:
<%
String name = "diego ";
Request. setAttribute ("name", name );
%>
<C: out value = "$ {name}"/>
......
Many JSTL labels support El expressions. Therefore, you can use other jstl labels as long as you insert values into your own labels into the request.
In the following example, get the object from the request, find its attribute value, and insert 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
<! -- 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>
Compile 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 its 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>
Run, the effect is as follows:
Get value from request and set its property value into request:
Now use OutTag of jstl taglib to get the name:
Value is: diego
Conclusion
Interaction with JSTL is a very useful technology. In JSTL, many labels are provided to complete basic functions, such as output, loop, and condition selection. You can implement your own labels only when processing your own specific logic, and provide interaction with jstl, which can greatly improve reusability and reduce workload.