On 11/27/05, Gaet <[EMAIL PROTECTED]> wrote:
> Hi the list!
>
> I'm looking for a taglib that allow to show and hide panels on a web page
> dynamically by simply cliking on a link.
>
>
> Do you know a such taglib?
>
>
> Thanks for sharing!

Here is my approach:
* When link is clicked, action class sets a view name in the request object
* Then it forwards to the JSP page
* JSP page displays a view corresponding to view name set in the request object

The tag class:
--------------

import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

public class RenderTag extends TagSupport {

  // View property of the render tag
  private String view;
  public String getView() { return view; }
  public void setView(String view) { this.view = view;}

  // Process start of the tag
  public int doStartTag() throws JspException {
    HttpServletRequest request = (HttpServletRequest)
(pageContext.getRequest());

    // Obtain name of a view to render from either page or request scope
    String selectedView = (String) pageContext.getAttribute("VIEW_TO_RENDER");
    if (selectedView == null || selectedView.length() == 0) {
      selectedView = (String) request.getAttribute("VIEW_TO_RENDER");
    }

    // Process tag body if "view" attribute is not specified (render anytime)
    // or "view" attribute defines the same view as VIEW_TO_RENDER request
    // attribute.
    if (view == null || view.length() == 0 || view.equals(selectedView)) {
      return EVAL_BODY_INCLUDE;
    }
  }

  // Process the end of this tag.
  public int doEndTag() throws JspException {
    return (EVAL_PAGE);
  }
}

The TLD file (I prefer JSP 1.2 or later for various reasons):
-------------------------------------------------------------

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
 PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
 "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd";>

<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>mytags</short-name>

    <tag>
        <name>render</name>
        <tag-class>RenderTag</tag-class>
        <body-content>JSP</body-content>
        <description>
            Renders view
        </description>
        <attribute>
            <name>view</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>

Usage example (action class):
-----------------------------

public ActionForward execute(ActionMapping mapping,
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response)
  throws Exception {

  // Process request
  ...

  // Select view to display
  HttpSession session = request.getSession();
  if (session.getAttribute("login.username") == null) {
    request.setAttribute("VIEW_TO_RENDER", "login");
  } else {
    request.removeAttribute("VIEW_TO_RENDER");
  }

  // Return proper ActionForward object, which forwards to JSP page
  ...
  return myActionForward;
}

Usage example (JSP page):
-------------------------

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/mytags.tld" prefix="mytags" %>
<html>

  <%-- Display login page only if the user has not logged in yet --%>
  <mytags:render view="login">
    ...
    Render your view here
    ...
  </mytags:render>

</html>

Michael.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to