1

In my project I am having a command link in which "target" attribute value is set to "_blank". So if validation error comes it will open a new window and show the error message.

But the requirement is if validation is correct it should target to new window otherwise it will remain in the same page and show the error message.

existing code :

    <p:commandLink value="View"
     styleClass="view_link" 
     ajax="false"
     action="#{bean.someMethod('preview')}" 
     target ="_blank"  />

My soln:

    <p:commandLink value="View"
    styleClass="view_link" 
    ajax="false" 
    action="#{bean.someMethod('preview')}"
    target="#{bean.target}">
    <p:ajax listener="#{bean.handelAjaxBehavoiurEvent}"/>
    </p:commandLink>

In the listner method I am validating the form and setting target value for p:commandlink. But it is not working.

Expected result : When I click on the link it should validate the form values. If values are correct then open a new tab in the browser and preview the pdf file. Else show the error message on the existing page.

It is why because while checking on the link itself it will open the new. I want to set the value of target attribute after form validation.

  • I definitely know why your solution above doesn't work but I have to think about a clever way to do what you want to do. – Melloware Jul 13 '18 at 12:58
  • @Melloware - I have a prime-faces dialogue and this command link inside it. When I click on command link I want to validate the form data. If data is valid then it should open a new tab and show the PDF. otherwise It would show error message in the existing dialogue. I executed a JavaScript function from managed bean and send the PDF data as base64 encoded data. But I need a better option. – Bal Vikash Sharma Jul 14 '18 at 16:52

1 Answers1

0

This is, how I do such things:

I have a xhtml-page to view any PDF (i.e. mypdfviewer.xhtml). The bean sets up some return args like validationOk, pdfname, viewoptions etc and returns these args via addCallbackParam(). In oncomplete I look for these values and, if ok, open the new tab.

Dialog:

<p:commandLink ajax="true" value="View" action="#{bean.someMethod('preview')}" oncomplete="return myPdfView(args)"/>

Bean:

public void someMethod(String what) {
  ...
  RequestContext.getCurrentInstance().addCallbackParam("validationOk", true);      
  RequestContext.getCurrentInstance().addCallbackParam("pdfFile", myfile);

JavaScript:

function myPdfView(args) {
  if(args && args.validationOk) {
    window.open("mypdfviewer.xhtml?file=" + args.pdfFile, '_blank');
  }
}
Holger
  • 899
  • 2
  • 7
  • 12
  • Thanks for your answer but, I am using jasper report to get the PDF. New JSF page is not the requirement. Every browser has its own format to preview the PDF. I have to use that only. – Bal Vikash Sharma Jul 17 '18 at 05:49
  • Your requirement: "open a new tab to preview the pdf" will be possible with `window.open(...)` – Holger Jul 17 '18 at 06:40
  • Yes I used that from manged bean. I placed that thing in my comments. But It not works well with all browser because I am passing Base64 encoded byteArray. – Bal Vikash Sharma Jul 17 '18 at 06:55
  • Hmm, I thought your problem is, to switch the correct target, not to show the pdf? – Holger Jul 17 '18 at 07:18
  • Yes It is but I don't want to use any extra JSF page. – Bal Vikash Sharma Jul 17 '18 at 12:43
  • I didn't write about an extra page. In `window.open()` you insert the name of the page you meant in your requirement: "If values are correct then open a new tab in the browser and preview the pdf file". If you open a new tab, you have to set *any* page to show in this new tab. – Holger Jul 17 '18 at 13:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176204/discussion-between-banti-kumar-and-holger). – Bal Vikash Sharma Jul 18 '18 at 04:18