Hello,

Please consider the following scenario: a page with a dataTable component
and a backing bean with two DataModel properties, one for the rows (binding
to the 'value' attribute of the dataTable) and one for columns (binding to
the 'value' attribute of the nested t:columns tag).  The backing bean is
configured in faces-config.xml to 'request' scope, but is being serialized
between requests by using the t:saveState tag (so that paging for example
would not require additional hits to the database).  So far everything is
working as expected.  However, problems arise when I try to implement a link
on the page that allows a user to open the table in a new window, and
without paging (for printing purposes).  The catch is that I want to be able
to still use the same backing bean so that the data doesn’t have to be
retrieved again, and also so that the sort would be preserved.  Could anyone
suggest the approach to take?  I tried the following:

Define a commandLink component and bind it to an action method on the
backing bean, like this:

    <h:outputLink value="searchView.faces" target="_blank"
class="linkCaption" rendered="#{workbenchForm.dataFound}">
        <t:graphicImage url="images/printer_icon.gif"
alt="#{msg.labelPrintQueueSumm}" width="21" height="14" /> 
        <h:outputText value="#{msg.labelPrintQueueSumm}"/>
    </h:outputLink><br/>

The body of the action method would then look like something like this:

  public void forwardToPrintView() throws IOException, ServletException {
      HttpServletRequest request = (HttpServletRequest)getRequest();
      HttpServletResponse response = (HttpServletResponse)getResponse();
      request.setAttribute("rows", rows.getWrappedData());
      request.setAttribute("columns", columns.getWrappedData());
      request.getRequestDispatcher("/workbenchPrint.faces").forward(request,
response);
      getFacesContext().responseComplete();        
  }

However this only works one time; after that I start seeing some really
weird behavior, like all links on the original page that were previously
working correctly start opening content in a new window?!  At first I though
there may be a problem with the target=”_blank” attribute, but when I
switched the commandLink component to an outputLink and had it point to
another page all together (without accessing the serialized backing bean)
the problem went away; could it be that the JSF state some how got
corrupted?  Any suggestions/clues would be greatly appreciated.

PS  I was able to get very similar functionality to work for "Exporting to
Excel"; the action method for this looks like this:

  public void exportHtmlTableToExcel() throws IOException{

      /*Set the filename
      DateTime dt = new DateTime();
      DateTimeFormatter fmt =
DateTimeFormat.forPattern("yyyy-MM-dd_HHmmss");
      String filename = dt.toString(fmt) + ".xls";*/
    
      //Setup the output
      String contentType = "application/vnd.ms-excel";
      FacesContext fc = FacesContext.getCurrentInstance();
      String filename = fc.getExternalContext().getUserPrincipal().getName()
+ "-"+ System.currentTimeMillis() + ".xls";
      HttpServletResponse response =
(HttpServletResponse)fc.getExternalContext().getResponse();
      response.setHeader("Content-disposition", "attachment; filename=" +
filename);
      response.setContentType(contentType);
    
      //Write the table back out
      PrintWriter out = response.getWriter();
      //First write column headings
      List columnList = (List)columns.getWrappedData();
      for (Iterator it = columnList.iterator(); it.hasNext(); ) {
          out.print(((TableColumnDTO)it.next()).getLabel() + "\t");
      }
      out.println();
      List data = (List)rows.getWrappedData();
      for (Iterator i = data.iterator(); i.hasNext(); ) {
          List row = (List)i.next();
          for (Iterator j = row.iterator(); j.hasNext(); ) {
              Object value = j.next();
              out.print((value != null ? value : "") + "\t");
          }
          out.println();
      }
      out.close();
      fc.responseComplete();
  }

One important difference to note is that the link for "Exporting to Excel"
does not have the target attribute set to '_blank' (since the content type
would force another application to handle it, thus opening it in a new
window automatically).
-- 
View this message in context: 
http://www.nabble.com/Implementing-Printable-View-for-dataTable%3A-Problem-with-target%3D%22_blank%22--tf2265822.html#a6287426
Sent from the MyFaces - Users forum at Nabble.com.

Reply via email to