Outils pour utilisateurs

Outils du site


helloworld:web:java:taglib:jsp

Ceci est une ancienne révision du document !


Nécessite un projet de type Dynamic Web Project.

tagperso.tld
<?xml version="1.0" encoding="UTF-8"?>
<j2ee:taglib version="2.0" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee"
  xmlns:xml="http://www.w3.org/XML/1998/namespace" 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 ">
  <j2ee:tlib-version>2.0</j2ee:tlib-version>
  <j2ee:short-name>tagperso</j2ee:short-name>
  <j2ee:tag>
    <j2ee:name>nomtag</j2ee:name>
    <j2ee:tag-class>com.llgc.TagPerso</j2ee:tag-class>
    <j2ee:body-content>JSP</j2ee:body-content>
    <j2ee:attribute>
      <j2ee:name>attribute1</j2ee:name>
      <j2ee:required>true</j2ee:required>
      <j2ee:rtexprvalue>true</j2ee:rtexprvalue>
    </j2ee:attribute>
    <j2ee:attribute>
      <j2ee:name>attribute2</j2ee:name>
      <j2ee:rtexprvalue>true</j2ee:rtexprvalue>
    </j2ee:attribute>
  </j2ee:tag>
</j2ee:taglib>

<note>rtexprvalue permet l'utilisation des EL dans les attributs.</note>

TagPerso.java
package com.llgc;
 
import java.io.IOException;
 
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
 
@SuppressWarnings ("serial")
public class TagPerso extends TagSupport
{
  private String attribute1;
  private String attribute2;
 
  public String getAttribute1 ()
  {
    return attribute1;
  }
 
  public void setAttribute1 (String attribute1)
  {
    this.attribute1 = attribute1;
  }
 
  public String getAttribute2 ()
  {
    return attribute2;
  }
 
  public void setAttribute2 (String attribute2)
  {
    this.attribute2 = attribute2;
  }
 
  @Override
  public int doEndTag () throws JspException
  {
    try
    {
      JspWriter out = pageContext.getOut ();
      out.println (".");
    }
    catch (IOException e)
    {
      e.printStackTrace ();
      throw new JspException (e);
    }
    return EVAL_PAGE;
  }
 
  @Override
  public int doStartTag () throws JspException
  {
    JspWriter out = pageContext.getOut ();
    try
    {
      out.println ("Bonjour, les attributs valent " + attribute1 + " et " + attribute2 + ".<br />");
      out.println ("Le corps du message : ");
    }
    catch (IOException e)
    {
      e.printStackTrace ();
      throw new JspException (e);
    }
    return EVAL_BODY_INCLUDE;
  }
}
taglib.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"
  import="java.util.List, com.llgc.BeanPersonne, com.llgc.Liste6"%>
<%@ taglib uri="WEB-INF/tagperso.tld" prefix="tag"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <p>
    <tag:nomtag attribute1="attr1">Corps</tag:nomtag>
  </p>
</body>
</html>

Rendu :

Bonjour, les attributs valent attr1 et null.
Le corps du message : Corps.
helloworld/web/java/taglib/jsp.1439457705.txt.gz · Dernière modification : 2015/08/13 11:21 de root