0

Im trying to send data from one jsf page to another, but without success.

firstPage.xhtml

<h:form>
<p:dataTable id="dataList" value="#{firstBean.dataList}" var="data">
<p:column>
<f:facet name="header">Column 1</f:facet>                            
#{data.column1.id}
</p:column>

<p:column>
<f:facet name="header" >Column 2</f:facet>                            
#{data.colum2.name}
</p:column>

<p:column>
<f:facet name="header" >Detail Row</f:facet>                            
<!-- I need this button send me to another page where i will detail information about an certain id -->
<h:commandButton value="Detalhar" action="#{firstBean.someMethod}">
<f:param name="dataId" value="#{data.column1.id}"/>
</h:commandButton>
</p:column>
</p:dataTable>
</h:form>

First Bean

@ManagedBean
@ViewScoped
public class FirstBean implements Serializable{

private static final long serialVersionUID = 1L;

//list with some data
private List<Data> dataList; //getter setter

@EJB
MyDataManager manager;

@PostConstruct
public void init() {
dataList = manager.getDataList();
}

public void someMethod() throws IOException {
//do something
FacesContext.getCurrentInstance().getExternalContext().redirect("secondPage.xhtml");
}
}

secondPage.xhtml

<h:body>
<f:metadata>
<f:viewParam name="dataId" value="#{secondBean.dataDetailId}" />
<f:viewAction action="#{secondBean.init}" />
</f:metadata>

<p:dataTable value="#{secondBean.dataDetailList}" var="dataDetail">
<p:column>
<f:facet name="header">Data Detail 1</f:facet>                            
#{dataDetail.data1}
</p:column>

<p:column>
<f:facet name="header">Data Detail 2</f:facet>                            
#{dataDetail.data2}
</p:column>

<p:column>
<f:facet name="header">Data Detail 3</f:facet>                            
#{dataDetail.data3}
</p:column>
</p:dataTable>
</h:body>

Second Bean

@ManagedBean
@ViewScoped
public class CarteiraDetalheBean implements Serializable{
private static final long serialVersionUID = 1L;

private Integer dataId; //getter setter
private List<DataDetail> dataDetailList; //getter setter


@EJB
MyDataManager manager;

@PostConstruct
public void init() {
dataDetailList = manager.getDataDetail(dataId);
}
}

When i use:

<f:viewAction action="#{secondBean.init}" />

I gotthe message: The metadata component needs to be nested within a f:metadata tag. Suggestion: enclose the necessary components within

and when i use:

<f:event type="preRenderView" listener="#{secondBean.init()}" />

i got a null pointer exception in line:

dataDetailList = manager.getDataDetail(dataId);

What im doing wrong?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
walves
  • 2,026
  • 6
  • 28
  • 46
  • Instead of FacesContext.getCurrentInstance().getExternalContext().redirect("secondPage.xhtml"); you could return just "secondPage" (the string) – Leo Aug 25 '14 at 18:27
  • In the second page, I think you need to add a f:metadata tag (before h:head) with tags f:viewParam (the param you're expecting) and f:event type="preRenderView" (the method that will handle it) – Leo Aug 25 '14 at 18:28
  • then in the second page MB, inside the method pointed by the preRenderView event, you can get the parameter you're sending using facesContext.getExternalContext().getRequestParameterMap().get("dataId"); – Leo Aug 25 '14 at 18:30

0 Answers0