如何在jsf中设置和获取viewparameter?

时间:2014-08-25 18:19:21

标签: jsf jsf-2 primefaces viewparams

我试图将数据从一个jsf页面发送到另一个页面,但没有成功。

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>

第二个豆

@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);
}
}

当我使用时:

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

我想知道消息:元数据组件需要嵌套在f:metadata标签中。建议:在

中包含必要的组件

当我使用时:

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

我在行中得到了一个空指针异常:

dataDetailList = manager.getDataDetail(dataId);

我做错了什么?

0 个答案:

没有答案