JS problem: You want to work together with JSTL. For example, after dealing with some logic with your own label, let Jstl handle the rest of the work.
Look at this JSP example:
<% String name= "Diego"; Request.setattribute ("name", name); %> ...... |
Many jstl tags support el expressions, so as long as you plug values into the request within your own tag, other jstl tags can use them
The following example, take the object from the request, find the value of its attributes, and shove it into 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) { 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; } } |
Writing TLD
!--setvartag--> Set Diegoyun. Setvartag Empty value true true Property false false var false false |
Writing 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 mans = New Man (); Man.setname ("Diego"); Request.setattribute ("Man", Mans); %> Get value from request and set it's property value into request:
Now use Outtag of Jstl taglib to get the name:
Value is:
|
Operation, 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 |
Conclusion
Interacting with Jstl is a very useful technique. The JSTL provides a number of tags that perform basic functions, such as output, loops, condition selection, and so on. Implementing your own tags only when dealing with your own specific logic and providing and jstl interaction can greatly improve reuse and reduce workload.