Java Tutorial/JSP/Custom Tag

Материал из Java эксперт
Версия от 17:44, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Custom Tag Accepting Fragments

Custom Tag As Template

Custom Tag Dynamic Attributes

Custom Tag For Display message

Custom Tag For Iteration

MyTag.java



package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport 
{
  private int counter = 0;
  private String[] names = null;
  public int doStartTag()
  {
    names = (String[]) pageContext.getAttribute("names");
    return EVAL_BODY_INCLUDE;
  }
  public int doAfterBody() throws JspException
  {
    try{
      pageContext.getOut().print(" " + names[counter] + "<BR>");
    } catch(Exception e){
      throw new JspException(e.toString());
    }
    counter++;
    if(counter >= names.length) {
      return SKIP_BODY;
    }
    return EVAL_BODY_AGAIN;
  }
}





Custom Tag For Outputing Message To Console

MyTag.java



package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport 
{
    public int doStartTag() throws JspException 
    {
        return EVAL_BODY_INCLUDE;
    }
    
    public int doAfterBody() throws JspException 
    {
            System.out.println("Hello!");
            return SKIP_BODY;
    }
}





Custom Tag For Shuffling Color

Custom Tag Set Bean Value

Custom Tag Setup Environment

Custom Tag Support

HelloWorld.java



package beans;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
public class HelloWorld extends TagSupport
{
    public int doStartTag() throws JspException
    {
        try
        {
            JspWriter out = pageContext.getOut();
            HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
            out.write("Hello world!");
        }
        catch(Exception e)
        {   
            throw new JspException(e.getMessage());
        }
        return EVAL_PAGE;
    }
}





Custom Tag To Get Parameter From PageContext

MyTag.java



package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport 
{
  private int counter = 0;
  private String[] names = null;
  public int doStartTag()
  {
    names = (String[]) pageContext.getAttribute("names");
    return EVAL_BODY_INCLUDE;
  }
  public int doAfterBody() throws JspException
  {
    try{
      pageContext.getOut().print(" " + names[counter] + "<BR>");
    } catch(Exception e){
      throw new JspException(e.toString());
    }
    counter++;
    if(counter >= names.length) {
      return SKIP_BODY;
    }
    return EVAL_BODY_AGAIN;
  }
}





Custom Tag To Insert Text

MyTag.java



package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport 
{
  public int doEndTag() throws JspException {
    try {
      pageContext.getOut().print("Hello from JSP!");
    } catch (Exception e) {
      throw new JspException(e.toString());
    } 
    return EVAL_PAGE;
  } 
}





Custom Tag Using Beans

Custom Tag Web URI Definition

Custom Tag Web XML Tag Lib

HelloWorld.java



package beans;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
public class HelloWorld extends TagSupport
{
    public int doStartTag() throws JspException
    {
        try
        {
            JspWriter out = pageContext.getOut();
            HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
            out.write("Hello world!");
        }
        catch(Exception e)
        {   
            throw new JspException(e.getMessage());
        }
        return EVAL_PAGE;
    }
}





Custom Tag With Attributes

Custom Tag Without TLD

Custom Tag With Property

MyTag.java



package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport 
{
  String text;
  public void setText(String s) {
    text = s;
  } 
  public int doEndTag() throws JspException {
    try {
      pageContext.getOut().print(text);
    } catch (Exception e) {
      throw new JspException(e.toString());
    } 
    return EVAL_PAGE;
  } 
}





Extends Tag Support DoEndTag

MyTag.java



package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport 
{
  public int doEndTag() throws JspException {
    try {
      pageContext.getOut().print("Hello from JSP!");
    } catch (Exception e) {
      throw new JspException(e.toString());
    } 
    return EVAL_PAGE;
  } 
}





Jars For Custom Tag Development

MyTag



package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport 
{
    public int doStartTag() throws JspException 
    {
        return EVAL_BODY_INCLUDE;
    }
    
    public int doAfterBody() throws JspException 
    {
            System.out.println("Hello!");
            return SKIP_BODY;
    }
}





Reference Tag Using Tag Dir

Jsp page with custom tag



<%@ taglib prefix="myTag" tagdir="/WEB-INF/tags" %>

<html>
<head>
<title>EL Type Conversion Examples</title>
</head>
<body>
<h1>EL Type Conversion Examples</h1>
<myTag:CharacterType val="this is it"/>
<myTag:CharacterType val="8"/>
<myTag:CharacterType val="3.0001"/>
<br/>
<myTag:DoubleType val=""/>
<myTag:DoubleType val="-3"/>
<br/>
<myTag:BooleanType val=""/>
<myTag:BooleanType val="true"/>
<myTag:BooleanType val="t"/>
<myTag:BooleanType val="3.1"/>

</body>
</html>





Repeat Tag

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package beans;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.util.HashMap;
import java.io.IOException;
/**
 * SimpleTag handler that accepts a num attribute and 
 * invokes its body "num" times.
 */
public class RepeatSimpleTag extends SimpleTagSupport {
    private int num;
    public void doTag() throws JspException, IOException {
        for (int i=0; i<num; i++) {
            getJspContext().setAttribute("count", String.valueOf( i + 1 ) );
      getJspBody().invoke(null);
        }
    }
    public void setNum(int num) {
  this.num = num;
    }
}