jsp标签解析与示例

其实以前也有写过自定义标签, 但是没有注意到过的用法, 最近这几天又用上自定义标签了, 突然发现的用法是有讲究的.

rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否可以使用JSP表达式.

当在标签里指定true时, 表示该自定义标签的某属性的值可以直接指定或者通过动态计算指定, example as follow:


select * from mytable order by nameid

<%request.setAttribute("nameid", "2"); %>




当在标签里指定false时, 表示该自定义标签的某属性的值只能直接指定, example as follow:


(部分内容转自: http://www.blogjava.net/echo/archive/2007/10/14/152821.html)

simple.tld




2.2.3
1.2
s
/simple

url
core.ui.UrlTag
empty

value
true
true



form
core.ui.FormTag
JSP

application
true
true


service
true
true



event
core.ui.EventTag
empty

event
true
true


name
true
true




此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;
}

/**
* 娣诲姞鏍囩涓棿鐨勫€煎:

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)