Taglib principle and realization: Re-discussion on El and Jstl labels

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.