毕业设计网
开发环境 |

jsp自定义标签的简单应用

 

Jsp自定义标签
1、演示viewIP
1) 演示jsp中java代码实现
2) 自定义标签实现
2、演示传统自定义标签(jsp2.0以前的)
1) 使用自定义标签控制页面内容(标签体)是否输出,利用doStartTag()的返回值控制
return this.SKIP_BODY; //忽略标签体
return this.EVAL_BODY_INCLUDE; //执行标签体
2) 控制整个jsp的输出
利用doEndTag()的返回值控制
return this.SKIP_PAGE; //跳过页面标签后余下的jsp代码
return this.EVAL_PAGE; //继续执行余下jsp代码
3) 自定义标签实现内容(标签体)循环输出
利用Tag子接口Iteration中定义的doAfterBody()和返回值EVAL_BODY_AGAIN,SKIP_BODY实现
a) 先覆盖doStartTag()方法,返回EVAL_BODY_INCLUDE
b) 覆盖doAfterBody()
public int doAfterBody() throws JspException {
times++;
int result = this.EVAL_BODY_AGAIN;
if(times>4){
result = this.SKIP_BODY;
}
return result;
}
4) 自定义标签修改内容(标签体)EVAL_BODY_BUFFERED;
标签处理类:
c) 继承BodyTagSupport
d) 覆盖doStartTag(),并返回EVAL_BODY_BUFFERED;
e) 覆盖doEndTag()
public int doEndTag() throws JspException {
BodyContent bc = this.getBodyContent();
String c = bc.getString();
c = c.toUpperCase();
JspWriter out = this.pageContext.getOut();
try {
out.write(c);
} catch (IOException e) {
throw new RuntimeException(e);
}
return this.EVAL_PAGE;
}
3、作业
实现一个自定义标签,
功能:判断一个YYYY-MM-DD格式的日期修改为下面格式输出
年:YYYY
月:MM
日:DD

	package com.csdn.view;


import java.io.IOException;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.JspWriter;

import javax.servlet.jsp.tagext.BodyContent;

import javax.servlet.jsp.tagext.BodyTagSupport;



public class ViewDateDemo extends BodyTagSupport {


@Override

public int doEndTag() throws JspException {

// TODO Auto-generated method stub


BodyContent bc= this.getBodyContent();

 String c = bc.getString();//以字符串类型获取那个body里的内容

String[] result =  c.split("-");//以-分数组,

 JspWriter out = this.pageContext.getOut();//获取out输出

 

        try {

out.print("年:"+result[0]+"<br>");//获取数组的第一个元素

out.print("月:"+result[1]+"<br>");//获取数组的第一个元素

out.print("日:"+result[2]+"<br>");//获取数组的第一个元素

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return EVAL_PAGE;

}




}


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

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"

    version="2.0">

    <description>A tag library exercising SimpleTag handlers.</description>

    <tlib-version>1.0</tlib-version>

    <short-name>csdn</short-name>

    <uri>http://www.csdn.com</uri>

    <tag>

<!--<description></description>-->

<name>ViewIP</name>

<tag-class>com.csdn.view.ViewIPTest</tag-class>

<body-content>empty</body-content>

    </tag>

    <tag>

<!--<description></description>-->

<name>ViewDate</name>

<tag-class>com.csdn.view.ViewDateDemo</tag-class>

<body-content>JSP</body-content>

    </tag>

    

</taglib>

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@taglib uri="http://www.csdn.com" prefix="csdn" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

    

    <title>My JSP 'viewDate.jsp' starting page</title


  </head>

  

  <body>

     <csdn:ViewDate>2012-11-17</csdn:ViewDate>

    

  </body>

</html>
以上是一部分介绍,如需要完整的资料或者如不符合您的要求,请联系技术人员qq:242219979咨询

上一篇:jsp上传图片即时显示效果代码
下一篇:jsp 编码问题


版权所有 毕业设计网联系qq:242219979 © 2007-2022