概述
其实以前也有写过自定义标签, 但是没有注意到过<rtexprvalue>的用法, 最近这几天又用上自定义标签了, 突然发现<rtexprvalue>的用法是有讲究的.
rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否可以使用JSP表达式.
当在<attribute>标签里指定<rtexprvalue>true</rtexprvalue>时, 表示该自定义标签的某属性的值可以直接指定或者通过动态计算指定, example as follow:
<sql:query var="result" >
select * from mytable order by nameid
</sql:query>
<%request.setAttribute("nameid", "2"); %>
<myTag:cupSize cupSize="1" cupSizes="${result}"></myTag:cupSize>
<myTag:cupSize cupSize="${nameid}" cupSizes="${result}"></myTag:cupSize>
当在<attribute>标签里指定<rtexprvalue>false</rtexprvalue>时, 表示该自定义标签的某属性的值只能直接指定, example as follow:
<myTag:cupSize cupSize="1" cupSizes="${result}"></myTag:cupSize>
(部分内容转自: http://www.blogjava.net/echo/archive/2007/10/14/152821.html)
simple.tld
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>2.2.3</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>s</short-name>
<uri>/simple</uri>
<tag>
<name>url</name>
<tag-class>core.ui.UrlTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>form</name>
<tag-class>core.ui.FormTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>application</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>service</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>event</name>
<tag-class>core.ui.EventTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>event</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
此tld文件可直接放在src文件的META-INFO目录中.这样就可避免在web.xml中再次设置tld的路径.
对应的java文件.
package core.ui;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.jsp.JspException;
public class TagBuilder {
private static final String SEP = " ";
//StringBuilder is not syschronize.
StringBuilder builder = new StringBuilder(100);
String name = null;
/**
*
* 銈炽兂銈广儓銉┿偗銈裤兗銆?
* @param name 鏍囩鐨勫悕瀛?
*/
public TagBuilder(String name) {
this();
this.name = name;
builder.append("<").append(name);
}
public TagBuilder() {
builder.append(SEP);
}
/**
* 娣诲姞灞炴€т俊鎭?
* @param name
灞炴€у悕
* @param v 灞炴€у€?
* @return
*/
public TagBuilder appendAttribute(String name, String v) {
builder.append(SEP);
// " 浠h〃 "
builder.append(name).append("=").append(""").append(v == null ? "" : v).append(""");
return this;
}
/**
* 缁撴潫灞炴€т俊鎭坊鍔?
* @return
*/
public TagBuilder endAppendAttribute() {
builder.append(">");
return this;
}
/**
* 娣诲姞鏍囩涓棿鐨勫€煎: <label>label<label>
* @param v
* @return
*/
public TagBuilder append(String v) {
builder.append(v == null ? "" : v);
return this;
}
/**
* 缁撴潫鏍囩
* @param simple
* @return
*/
public TagBuilder endTag(boolean simple) {
if (simple) {
builder.append(" />");
} else {
if (name == null)
throw new IllegalArgumentException();
builder.append("</").append(name).append(">");
}
return this;
}
/**
* 杈撳嚭鏍囩鍐呭
* @param writer
* @throws JspException
*/
public void writeTo(Writer writer) throws JspException {
try {
writer.write(builder.toString());
} catch (IOException e) {
throw new JspException("IO Error", e);
}
}
public String toString() {
return builder.toString();
}
}
package core.ui;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class UrlTag extends TagSupport{
private static final long serialVersionUID = 6920029415011588731L;
String value = null;
/**
* value傪庢摼偡傞丅
* @return value
*/
public String getValue() {
return value;
}
/**
* value傪愝掕偡傞丅
* @param value value
*/
public void setValue(String value) {
this.value = value;
}
public UrlTag() {}
@Override
public int doEndTag() throws JspException {
if (value == null) throw new JspException();
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
String context = request.getContextPath();
if (context.length() > 1) {
value = context + value;
}
try {
pageContext.getOut().write(value);
} catch (IOException e) {
throw new JspException("IO Exception", e);
}
return EVAL_PAGE;
}
@Override
public void release() {
value = null;
super.release();
}
}
部分源代码:
最后
以上就是自由石头为你收集整理的jsp标签解析与示例的全部内容,希望文章能够帮你解决jsp标签解析与示例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复