java JSP自定义标签

时间:2022-09-16 05:28:11

来至: http://blog.csdn.net/jiangwei0910410003/article/details/23915373

http://blog.csdn.net/jiangwei0910410003/article/details/23915373

--------------------------

jsp自定义标签 Tag文件版

实现一个与上篇文章类似的Select标签功能
1.在WEB-INF/tags/select.tag

<%@ tag body-content="empty" %>
<%@ tag dynamic-attributes="tagAttrs" %>
<%@ attribute name="optionsList" type="java.util.List" required="true" rtexprvalue="true"%>
<%@ attribute name="name" required="true"%>
<%@ attribute name="size" required="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>

<select name="${name }" size="${size }"
   <c:forEach var="attrEntry" items="${tagAttrs }">
     ${attrEntry.key}="${attrEntry.value }"
   </c:forEach>
>
   <c:forEach var="option" items="${optionsList}">
     <option value="${option }">${option}</option>
    </c:forEach>
 </select>
这里要注意tag文件只能放在如下位置:
1.WEB-INF/tags
2.WEB-INF/tags的子目录
3.WEB-INF/lib中jar包的META-INF/tags
4.WEB-INF/lib中jar包的META-INF/tags下的子目录
5.jar包中的tag文件需要tld
添加jstl.jar与standard.jar到WEB-INF/lib目录,还有一点就是上面标红的部分:不要使用http://java.sun.com/jstl/core这个url,否则会报foreach中的item属性有问题

2.在jsp中的使用

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ taglib prefix="formTag" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
 List<String> colorList = new ArrayList<String>();
    colorList.add("red");
    colorList.add("blue");
    colorList.add("white");
    request.setAttribute("colorList",colorList);
%>

<form action="" method="post">
 <formTag:select name="color" size="1" optionsList="${requestScope.colorList}"  style="width:140px"/>
</form>
</body>
</html>

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

jsp 自定义标签

jsp标签有两组api
JspTag ->SimpleTag ->SimpleTagSupport
JspTag ->Tag ->IterationTag->BodyTag
第二组是classic的,比较早的使用方式,doStartTag(),doEndTag()有N多返回值的那种,使用起来也确实不方便,今天学到了另一个使用第一组api方式的,让人大快人心,贴码
例子是一个Select的标签,支持动态属性设置
1.编写标签类

public class SelectTagHandler extends SimpleTagSupport implements DynamicAttributes {
 private static final String ATTR_TEMPLATE = "%s='%s'";
 private static final String OPTION_TEMPLATE = "<option value='%1$s'>%1$s</option>";
 private List optionsList;
 private String name;
 private String size;
 private Map<String, Object> tagAttrs = new HashMap<String, Object>();

public void setName(String name) {
  this.name = name;
 }

public void setSize(String size) {
  this.size = size;
 }

public void setOptionsList(List optionsList) {
  this.optionsList = optionsList;
 }

@Override
 public void doTag() throws JspException, IOException {
  PageContext pageContext = (PageContext) getJspContext();
  JspWriter out = pageContext.getOut();
  out.print("<select ");
  out.print(String.format(ATTR_TEMPLATE, "name", this.name));
  out.print(String.format(ATTR_TEMPLATE, "size", this.size));
  for (String attrName : tagAttrs.keySet()) {
   String attrDefinition = String.format(ATTR_TEMPLATE, attrName, tagAttrs.get(attrName));
   out.print(attrDefinition);
  }
  out.print(">");

for (Object option : this.optionsList) {

String optionTag = String.format(OPTION_TEMPLATE, option.toString());
   out.println(optionTag);
  }
  out.println("</select>");
 }

@Override
 public void setDynamicAttribute(String uri, String name, Object value) throws JspException {
  tagAttrs.put(name, value);
 }
}
看到没,代码如此的简洁,动态属性配置也十分的方便,不用写N多个setter与getter方法.

2.编写tld文件WebRoot/tld/select.tld

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3g.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"
version="2.0">
 <tlib-version>1.2</tlib-version>
 <jsp-version>1.2</jsp-version>
 <short-name>Forms Taglib</short-name>
 <uri>http://hi.baidu.com/tags/forms</uri>
 <description>
  An example tab library of replacements for the html form tags.
 </description>
 
 <tag>
  <name>select</name>
  <tag-class>com.baidu.hi.tag.SelectTagHandler</tag-class>
  <body-content>empty</body-content>
  
  <attribute>
   <name>optionsList</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
   <type>java.util.List</type>
  </attribute>
  
  <attribute>
   <name>name</name>
   <required>true</required>
  </attribute>
  
  <attribute>
   <name>size</name>
   <required>true</required>
  </attribute>
  
  <dynamic-attributes>true</dynamic-attributes>
 </tag>
</taglib>

3.在jsp中的使用

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.util.*" %>
<%@ taglib  prefix="formTags"  uri="/tld/select.tld"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="java.util.ArrayList"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
 List<String> colorList = new ArrayList<String>();
    colorList.add("red");
    colorList.add("blue");
    colorList.add("white");
    request.setAttribute("colorList",colorList);
%>
<form action="" method="post">
 <formTags:select name="color" size="1" optionsList="${requestScope.colorList}" style="width:140px"/>
</form>
</body>
</html>