jsp自定义标签的简单编写

本例标签的功能是判断所给的字符串是否大于指定长度,如大于则截取,用于对页面上过长字符的处理

tld文件,在WEB-INF下的tlds(自行创建)文件夹创建tld文件,

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.1</tlib-version>
<short-name>page</short-name>
<uri>http://java.StringSub.org/jsp/jstl/ssb</uri>
<tag>
<name>StringSub</name>
<!-- 处理Bean -->
<tag-class>com.tld.StringSubTag</tag-class>
<body-content>JSP</body-content>
<!-- 最大长度 -->
<attribute>
<name>maxLength</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.Integer</type>
</attribute>
<!-- 需要判断的字符串 -->
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>

 

处理Bean

package com.tld;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class StringSubTag extends BodyTagSupport {
private static final long serialVersionUID = -9082475690512988559L;
private int maxLength;
private String value;

//处理函数
//bodyContent.getString()函数为获取标签体包含的部分,如<ss:StringSub maxLength="12">bodyContent.getString()获取我</ss:StringSub>
public int doEndTag ()throws JspException {
if (bodyContent.getString().trim().length()>0) {
this.setValue(bodyContent.getString().trim());
}
JspWriter out = pageContext.getOut();
if(null==value){
value = "";
}
try {
int vLen = value.length();
if(vLen>maxLength){
value=value.substring(0,maxLength)+"...";
}
out.print(value);
} catch (Exception e) {
e.printStackTrace();
}
return EVAL_PAGE;
}

public int getMaxLength() {
return maxLength;
}
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}


}

 

web.xml配置

    <!-- web.xml中只可有1个 jsp-config标签,多个自定义标签配置写在taglib中-->
<jsp-config>
<taglib>
<taglib-uri>/tags/StringSub.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/StringSub.tld</taglib-location>
</taglib>
</jsp-config>


jsp中使用

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/tags/StringSub.tld" prefix="ss"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<!-- 结果输出12,用Struts2标签的童鞋,请用第2种,第1种会报错嘀。。。 -->

<!-- 赋值方式1 -->
<ss:StringSub maxLength="2" value="123456"></ss:StringSub>
<!-- 赋值方式2 -->
<ss:StringSub maxLength="2">123456</ss:StringSub>
</body>
</html>







 

posted @ 2012-02-04 11:10  将来的老大爷  阅读(299)  评论(0编辑  收藏  举报

如果本页面列出的内容侵犯了您的权益,请告知。
知识共享许可协议
996.icu