1

f:setPropertyActionListener in p:commandLink

How can I bring the data from the setter of the target- Attribut to the getter of the surrounding onclick-attribute?

I want to build an hyperlink within a p:CommandLink (onclick = "window.open('http://www.somewebsite.de ...')" for each row of the datatable. So the Request-parameters of "somewebsite" change depending on the actual row. I have a working p:commandLink, because I can generate an new browser-window but only with a (constant! [and that is the problem/challenge]) website. I can also access the needed data from EACH row through the p:commandLink that means the belonging data which I need for the hyperlink-Request-parameters, but I cannot bring it together, because I get a Nullpointer-Exception.

Look the spot which is marked with // HERE SEEMS TO BE THE PROBLEM

<p:dataTable value="#{columnsViewZwei.dataList1}" var="row"
           resizableColumns="true"
              styleClass="foo"
           tableStyleClass="maggtabl2"
           tableStyle="maggtabl2"
           headerClass="order-table-header"
           rowClasses="order-table-odd-row,order-table-even-row">
          
         <p:column headerText="Titel" sortBy="#{row.titel}" filterBy="#{row.titel}">
                  <h:outputText value="#{row.titel}" />
            </p:column>
            <p:column headerText="Typ" sortBy="#{row.typ}" filterBy="#{row.typ}">
                  <h:outputText value="#{row.typ}" />
            </p:column>

            <p:column headerText="Metadaten" style="width:64px;text-align: center">
                  
            <p:commandLink value="show" title="show metatdata" onclick="#{columnsViewZwei.testurl}" >
                <f:setPropertyActionListener value="#{row}" target="#{columnsViewZwei.selectedSET}" />
            </p:commandLink>           
            </p:column> 
</p:dataTable>

package bbb;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.xml.stream.XMLStreamException;
import bbb.InspireDS;
 
@ManagedBean
@ViewScoped
public class ColumnsViewZwei implements Serializable {

    
private String testurl = "";
    
private InspireDS selectedDataset;

public InspireDS getSelectedDataset() {
  System.out.println("getSelectedDataset() ");
  return this.selectedDataset;
}

public String selectedDataset() {
  selectedDataset = getSelectedDataset();
  this.testurl = "window.open('..Info.do?fileIdentifier="+selectedDataset.metadaten+"')";
  return testurl;
}

public void setTesturl(String testurl){
 this.testurl = "window.open('http://...Info.do?fileIdentifier="+selectedDataset.metadaten+"')";
 }

public String getTesturl() {
  //selectedDataset = getSelectedDataset();
  // HERE SEEMS TO BE THE PROBLEM:  NullPointerException probably for selectedDataset
  //this.testurl = "window.open('..?fileIdentifier="+selectedDataset.metadaten+"')";
  
  this.testurl = "window.open(' ...')"; // works, but constant hyperlink
  return testurl;
}

// This from f:setPropertyActionListener value="#{row}" target="#{columnsViewZwei.selectedSET}
// brings the data I need, but it should be accessable in getTesturl()
public void setSelectedSET(InspireDS inspirDS) {     
   this.selectedDataset = inspirDS;
  this.testurl = "window.open('...do?fileIdentifier="+selectedDataset.metadaten+"')";
}
    
private List<InspireDS> dataList1;
    
@ManagedProperty("#{inspireTabelleBean}")

private InspireTabelleBean insTabBean;

@PostConstruct
public void init() {
    try {
     if(insTabBean==null){System.out.println("s e r v i ce = =  n u ll");}
   dataList1 = insTabBean.fillListbyparse();
   
  } catch (XMLStreamException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
    }
    
public void setService(InspireTabelleBean service) {
        this.insTabBean = service;
}
    
public List<InspireDS> getDataList1() {
        return dataList1;
}
    
public void setInsTabBean(InspireTabelleBean service) {
        this.insTabBean = service;
}
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
tsitra
  • 11
  • 3

2 Answers2

1

I'm not able to reproduce your code, i did it a simplified version without datatable.

Instead of window.open function i simply used target="_blank" as explained in this this post.

The error you made is use onclick coupled with f:setPropertyActionListener. The reason why that doesn't function is explained in this post.

faces-redirect=true is needed for change url (read more on this post), and ajax must be false.

However, when you want call bean function through js, take a look to remote command.

xhtml Page:

<h:form id="form_open_window" target="_blank">
    <p:commandLink value="show" title="show metatdata" action="#{columnsViewZwei.goToUrl()}" ajax="false" >
        <f:setPropertyActionListener value="metadata_parameter" target="#{columnsViewZwei.metadata}" />
    </p:commandLink>
</h:form>

Bean Code:

private String testurl = "index";

private String metadata;



public String getMetadata() {
return metadata;
}



public void setMetadata(String metadata) {
this.metadata = metadata;
}



public String goToUrl() {
System.out.println(metadata);
return testurl + "?faces-redirect=true&fileIdentifier=" + metadata;
}

PS: If You don't want deviate too much from your solution, evaluate use of oncomplete instead of onclick, but i didn't try it.

I hope this is useful.

Community
  • 1
  • 1
giaffa86
  • 708
  • 1
  • 9
  • 22
0

and thank you for your fast answer. I used your suggestions, but what happend was not what i want. (The table was distorted etc.) I put in each Method (getter and setter) the System.out.println(...) as you did for goToUrl() {...}, but I did not see any printouts on the console. So it is mysterious which code was invoked.

When I clicked on the commandLink I got a new window with the same facelet "index.xhtml" as i started from. I guess this is the effect of testurl = "index"; My view-file (facelet) has the name "index.xhtml".

I think it is good that you mentioned the other posts so I can try to understand your arguments.

I suppose that I need the value="#{row}" in the f:setPropertyActionListener to access the current row/dataset because in this current row/dataset is the information which i need to build the url to call/access a remote website.

With kind regards, Yours sincerely tsitra

ooooooooooooooooo date:22.12.2014 A possible solution without using a Primefaces component ( control ) was :

 <p:dataTable value="#{columnsViewZwei.dataList1}" var="o9"
       resizableColumns="true">
      
        <p:column headerText="Titel" sortBy="#{o9.titel}" filterBy="#{o9.titel}">
              <h:outputText value="#{o9.titel}" />
          </p:column>
          <p:column headerText="Typ" sortBy="#{o9.typ}" filterBy="#{o9.typ}">
              <h:outputText value="#{o9.typ}" />
          </p:column>
          <p:column headerText="Metadaten">
           <a href="https://geoportal.bafg.de/portal/Query/ShowCSWInfo.do?fileIdentifier=#{o9.identifier}" target="_blank">show</a>
          </p:column>
 </p:dataTable>
tsitra
  • 11
  • 3