当前位置: 代码迷 >> JavaScript >> jsp自定义标签的简略应用及作业
  详细解决方案

jsp自定义标签的简略应用及作业

热度:145   发布时间:2012-11-23 22:54:33.0
jsp自定义标签的简单应用及作业

Jsp自定义标签

1、演示viewIP

1) 演示jspjava代码实现

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>

  相关解决方案