0

I have to perform two operations on button click.

  1. call a method in a bean which will return a url and it will be set in some variable.

  2. after that I have to open that url in another tab. I am using the action attribute of CommandButton and it is setting the URL value correctly, but how do I open that url as pdf in a new tab. i.e "URL of pdf should open in a new tab"

Code sample as follows:

<div class="form-options">
    <h:commandButton value="Download Report" styleClass="btn btn-primary btn-icon buttonCustom" actionListener="#{reportBean.GenerateReport}">
    </h:commandButton>
</div>  
zytham
  • 613
  • 1
  • 12
  • 27

3 Answers3

2

Use commandLink instead of commandButton.. When commandLink is used you need to provide value for target attribute as new window.

<h:commandLink target="new window">

Rest all the code will be as per your requirement. Since the URL in which your report is present is already present in one of the variables you can use this variable from the bean in commandLink to provide the URL..

Elsewhile you can use an iframe also.It is generally used to display the report on the same page...

AngelsandDemons
  • 2,823
  • 13
  • 47
  • 70
2

Hope I unterstood you right. This worked for me:

  1. Provide the method generating the report and the URL in the actionListener-attribute of the commandButton (what you already did, according to your sample code).
  2. Provide the method returning the URL in the action-attribute of the commandButton (it will be called after the actionListener-method).
  3. For opening a new tab, add target="_blank in the parent <h:form> (taken from this questions answer)

XHTML-Code:

<h:form target="_blank">
    <h:commandButton value="Show Report" 
                     actionListener="#{reportBean.generateReport}"
                     action="#{reportBean.getUrl}" />
</h:form>

As this approach applies the target="_blank" to all non-ajax calls, here's another way using Javascript:

<h:form>
    <h:commandButton value="Show Report" 
                     actionListener="#{reportBean.generateReport}"
                     action="#{reportBean.getUrl}" 
                     onclick="this.form.target='_blank'"/>
</h:form>

Java-Code:

public void generateReport(final ActionEvent evt) {
    // ... generating report and setting url-variable
}


public String getUrl() {
    return url;
}

If you have to forward to an external link, see this question

Community
  • 1
  • 1
Janis K.
  • 316
  • 1
  • 2
  • 7
  • if i have to pass two parameter from jsf to bean then it will work ? – zytham Aug 22 '12 at 10:02
  • like i have to call a method with two argument like #{method_name(arg1,arg2)} – zytham Aug 22 '12 at 10:02
  • Seems like [this](http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/) is the way to go for you. I suggest you try approach 3 (using f:attribute). This should work for you, but since I do not know what kind of parameters you want to pass and where they come from, I can only guess. – Janis K. Aug 22 '12 at 10:10
2

As I understand, your problem is not about redirecting the result of the action to a new tab, instead you want to show a report (if my guessing is right, you're using a JasperReport and want to show it in PDF format). To do this, you must download the file to the client computer and then let the default editor/program on the client handle the file.

I won't go into details for file download, BalusC provides good info about this matter:

The relevant part to show PDF (or any other) files in browser is the ContentType of the response. This will say to the browser how the file must be threatened. Some content type values

  • PDF: application/pdf
  • Excel: application/vnd.ms-excel
  • Microsoft Word: application/ms-word
  • More: Internet media type

In case I'm wrong and you don't want to show (download) the file and need to open the file in a new Window, you have two ways (resuming both @JanisK. and @AngelsandDemons answers):

  • Setting the target of the form as "_blank", but this way will do that all the performed actions will open a new tab/window.

    <h:form target="_blank">
        <h:commandButton ... />
    </h:form>
    
  • Using a commandLink instead of a commandButton, where you can set the target only for this link requests.

    <h:form>
        <h:commandLink target="_blank" ... />
    </h:form>
    
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Luiggi Mendoza Thumb Up :) i am using jasper report .. it worked now thanks. – zytham Aug 24 '12 at 08:28
  • i used this : try { FacesContext.getCurrentInstance().getExternalContext().redirect(filenameonly); } catch (IOException ex) { // do something here } – zytham Aug 24 '12 at 08:34
  • @zytham Why would you redirect to your a path with your filename? That's plain wrong. It seems like you haven't read the links in my post. – Luiggi Mendoza Aug 24 '12 at 08:49
  • no i just make it work like that..but i am trying with that ur link with stream approach... – zytham Aug 24 '12 at 10:43