Changeset 5485


Ignore:
Timestamp:
Nov 11, 2010, 8:57:08 AM (12 years ago)
Author:
Nicklas Nordborg
Message:

References #1545: Add extension points to edit pages

Rearrange the code so that the included page is not called until the contents is actually requested.

Store the JspContext? in a request-scoped attribute so that the including page can access the information in it.

Location:
trunk/src/clients/web/net/sf/basedb/clients/web/extensions
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/clients/web/net/sf/basedb/clients/web/extensions/JspContext.java

    r4872 r5485  
    7272{
    7373 
     74  /**
     75    The key that is used when storing a JspContext object in
     76    a request/page/session/etc. attribute.
     77    @since 2.17
     78  */
     79  public static final String ATTRIBUTE_KEY = "net.sf.basedb.clients.web.extensions.JspContext";
     80 
    7481  private final ExtensionsDirectory directory;
    7582  private final PageContext pageContext;
  • trunk/src/clients/web/net/sf/basedb/clients/web/extensions/tabcontrol/IncludeContentTabFactory.java

    r5483 r5485  
    2222package net.sf.basedb.clients.web.extensions.tabcontrol;
    2323
     24import javax.servlet.ServletRequest;
    2425import javax.servlet.jsp.PageContext;
    2526import javax.servlet.jsp.tagext.BodyContent;
     
    3940  as the content of tab. The other resource can be a JSP
    4041  script, servlet, a static file, or anything else that makes sense.
     42  <p>
     43  The current {@link JspContext} is stored as a {@link ServletRequest} attribute
     44  with key {@link JspContext#ATTRIBUTE_KEY}. If the included page is a JSP page it
     45  can get the JspContext by calling
     46  <code>request.getAttribute(JspContext.ATTRIBUTE_KEY)</code>.
    4147 
    4248  <p>
     
    7985  public TabAction[] getActions(InvokationContext context)
    8086  {
    81     JspContext jsp = (JspContext)context.getClientContext();
    82     PageContext pageContext = jsp.getPageContext();
    83     String tabContent = null;
    84     BodyContent tabBody = null;
    85     try
    86     {
    87       tabBody = pageContext.pushBody();
    88       pageContext.include(include);
    89       tabContent = tabBody.getString();
    90     }
    91     catch (Exception ex)
    92     {
    93       tabContent = "<div class=\"stacktrace\">" +
    94         HTML.encodeTags(ThrowableUtil.stackTraceToString(ex)) +
    95         "</div>";
    96     }
    97     finally
    98     {
    99       if (tabBody != null) pageContext.popBody();
    100     }
    101    
    102     Tab tab = new Tab(tabContent);
     87    Tab tab = new Tab((JspContext)context.getClientContext());
    10388    return new TabAction[] { tab };
    10489  }
     
    167152  {
    168153
    169     private final String content;
     154    private final JspContext jspContext;
    170155   
    171     Tab(String content)
    172     {
    173       this.content = content;
     156    Tab(JspContext jspContext)
     157    {
     158      this.jspContext = jspContext;
    174159    }
    175160   
     
    235220    public String getContent()
    236221    {
    237       return content;
    238     }
    239 
     222      PageContext pageContext = jspContext.getPageContext();
     223      String tabContent = null;
     224      BodyContent tabBody = null;
     225      try
     226      {
     227        tabBody = pageContext.pushBody();
     228        pageContext.getRequest().setAttribute(JspContext.ATTRIBUTE_KEY, jspContext);
     229        pageContext.include(include);
     230        tabContent = tabBody.getString();
     231      }
     232      catch (Exception ex)
     233      {
     234        tabContent = "<div class=\"stacktrace\">" +
     235          HTML.encodeTags(ThrowableUtil.stackTraceToString(ex)) +
     236          "</div>";
     237      }
     238      finally
     239      {
     240        if (tabBody != null) pageContext.popBody();
     241      }
     242      return tabContent;
     243    }
    240244  }
    241245 
Note: See TracChangeset for help on using the changeset viewer.