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"?>
<javaee:taglib version="2.1" xmlns:javaee="http://java.sun.com/xml/ns/javaee"
  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/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd ">
  <javaee:tlib-version>2.1</javaee:tlib-version>
  <javaee:short-name>tagperso</javaee:short-name>
  <javaee:tag>
    <javaee:name>nomtag</javaee:name>
    <javaee:tag-class>com.llgc.TagPerso</javaee:tag-class>
    <javaee:body-content>JSP</javaee:body-content>
    <javaee:attribute>
      <javaee:name>attribute1</javaee:name>
      <javaee:required>true</javaee:required>
      <javaee:rtexprvalue>true</javaee:rtexprvalue>
    </javaee:attribute>
    <javaee:attribute>
      <javaee:name>attribute2</javaee:name>
      <javaee:rtexprvalue>true</javaee:rtexprvalue>
    </javaee:attribute>
  </javaee:tag>
</javaee: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"%>
<%@ 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.1439485149.txt.gz · Dernière modification : 2015/08/13 18:59 de root