当前位置: 代码迷 >> JavaScript >> JSP自定义标签的兑现
  详细解决方案

JSP自定义标签的兑现

热度:480   发布时间:2012-10-29 10:03:53.0
JSP自定义标签的实现
package test.util;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class DateTag implements Tag {

    private PageContext pc;
 
 public int doEndTag() throws JspException {
 
    Calendar c=Calendar.getInstance();
     int year=c.get(Calendar.YEAR);
     int month=c.get(Calendar.MONTH)+1;
     int day=c.get(Calendar.DATE);
     try {
   this.pc.getOut().println(year+"年"+month+"月"+day+"日");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }  
  return 0;
 }


 public int doStartTag() throws JspException {
 
  return 0;
 }


 public Tag getParent() {

  return null;
 }


 public void release() {


 }


 public void setPageContext(PageContext arg0) {
 
         this.pc=arg0;
 }


 public void setParent(Tag arg0) {
 
 }

}

?

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

<taglib 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"
    version="2.1">

 <tlib-version>1.0</tlib-version>
 <short-name>stu</short-name>
 <uri>getDate</uri>
 
 <tag>
 <name>getDate1</name>
 <tag-class>test/util/DateTag</tag-class>
 <body-content>empty</body-content>
 </tag>
 </taglib>

?

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.text.DateFormat"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@taglib prefix="stu" uri="getDate" %>
<% Date d=new Date();
   DateFormat df=new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss  a  E");
  
   Calendar c=Calendar.getInstance();
   int year=c.get(Calendar.YEAR);
   int month=c.get(Calendar.MONTH)+1;
   int day=c.get(Calendar.DATE);
   out.println(year+"年"+month+"月"+day+"日");
  
 %>
 <br/>
 <dateTag:getDate/>

?

  相关解决方案