JSP 自定义标签示例

package com.lixing.mobile_scm.web;

import java.io.IOException;

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

public class NavigateTag extends SimpleTagSupport {

         private int percent = 100;
         private int width = 1000;
         private String text;
         private String widthType = "percent";

         public void setPercent( int percent) {
                 this.percent = percent;
        }

         public void setText(String text) {
                 this.text = text;
        }

         public void setWidth( int width) {
                 this.widthType = "width";
                 this.width = width;
        }

        @Override
         public void doTag() throws JspException, IOException {
                JspWriter out = getJspContext().getOut();
                String tableWidth = this.widthType.equals( "percent") ? (percent + "%")
                                : width + "";
                out.print( "<table width='" + tableWidth
                                + "' border='0' cellspacing='0'"
                                + " cellpadding='0' align='center'><tr>");
                out.print( "<td class='navTd1'></td>");
                out.print( "<td class='navTd2'>" + text + "</td>");
                out.print( "<td class='navTd3'></td></tr></table>");
        }

}


<? 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.1 </ tlib-version >
   < short-name >cust </ short-name >
   < uri >/cust-tags </ uri >

   < tag >
     < name >navigation </ name >
     < tag-class >com.lixing.mobile_scm.web.NavigateTag </ tag-class >
     < body-content >empty </ body-content >
     < attribute >
       < name >percent </ name >
       < required >false </ required >
       < rtexprvalue >true </ rtexprvalue >
     </ attribute >
     < attribute >
       < name >width </ name >
       < required >false </ required >
       < rtexprvalue >true </ rtexprvalue >
     </ attribute >
     < attribute >
       < name >text </ name >
       < required >true </ required >
       < rtexprvalue >true </ rtexprvalue >
     </ attribute >
   </ tag >
</ taglib >



你可能感兴趣的:(jsp,职场,休闲,自定义标签示例)