jsp自定义标签的简单编写
2012-02-04 11:10
337 查看
本例标签的功能是判断所给的字符串是否大于指定长度,如大于则截取,用于对页面上过长字符的处理
tld文件,在WEB-INF下的tlds(自行创建)文件夹创建tld文件,
处理Bean
web.xml配置
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>
相关文章推荐
- jsp自定义标签的简单应用及作业
- 在你用JSP-servlet编写程序时候可以用以下简单的乱码处理解决!!
- JSP自定义标签就是如此简单
- 使用js+jquery编写一简单的数据库查询界面(单jsp界面完成)
- 简单的Jsp自定义标签
- JSP tag文件配置 自定义标签简单示例
- 最简单的一个jsp自定义标签
- JSP自定义标签就是如此简单
- jsp自定义标签(taglib)编写的原理和实现
- jsp 自定义标签 一 简单示例
- JSP自定义标签(1):一个简单的标签
- JSP tld文件配置 自定义标签简单示例
- jsp自定义标签(taglib)编写的原理和实现【转载】
- 【JSP】赵雅智_JSP自定义标签_简单标签
- JSP自定义标签就是如此简单
- jsp代码编写简单的BBS论坛项目的总结
- jsp中用自定义标签实现简单的分页
- JSP简单的一个自定义标签
- jsp自定义标签case、when的简单实现
- JSP的JNDI简单编写