You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Gaet <ga...@free.fr> on 2005/11/28 08:40:40 UTC

Hide/Show panels dynamically???

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!

 

Re: Hide/Show panels dynamically???

Posted by Michael Jouravlev <jm...@gmail.com>.
On 11/28/05, Gaet <ga...@free.fr> wrote:
> Thanks Michael for the time you have taken to reply me!
>
> I will study your solution deeper...thanks!

You are welcome. And here goes a correction:

Change this:

    if (view == null || view.length() == 0 || view.equals(selectedView)) {
     return EVAL_BODY_INCLUDE;
   }

to this:

    if (view == null || view.length() == 0 || view.equals(selectedView)) {
     return EVAL_BODY_INCLUDE;
   } else {
     return SKIP_BODY;
   }

Have fun,
   Michael.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Hide/Show panels dynamically???

Posted by Gaet <ga...@free.fr>.
Thanks Michael for the time you have taken to reply me!

I will study your solution deeper...thanks!

Sincerely...

 
  ----- Original Message ----- 
  From: Michael Jouravlev 
  To: Struts Users Mailing List 
  Sent: Monday, November 28, 2005 6:13 PM
  Subject: Re: Hide/Show panels dynamically???


  On 11/27/05, Gaet <ga...@free.fr> 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: user-unsubscribe@struts.apache.org
  For additional commands, e-mail: user-help@struts.apache.org


Re: Hide/Show panels dynamically???

Posted by Michael Jouravlev <jm...@gmail.com>.
On 11/27/05, Gaet <ga...@free.fr> 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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org