我正在尝试在两个xhtml之间传递参数,我有第一个xhtm的下一个代码
<h:dataTable value="#{ofertaController.items}" var="item" border="0" cellpadding="2" cellspacing="0" rowClasses="jsfcrud_odd_row,jsfcrud_even_row" rules="all" style="border:solid 1px">
<h:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListOfertaTitle_idoferta}"/>
</f:facet>
<h:outputText value="#{item.idoferta}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListOfertaTitle_fechaInicio}"/>
</f:facet>
<h:outputText value="#{item.fechaInicio}">
<f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss" />
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListOfertaTitle_fechaFin}"/>
</f:facet>
<h:outputText value="#{item.fechaFin}">
<f:convertDateTime pattern="MM/dd/yyyy HH:mm:ss" />
</h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListOfertaTitle_descripcion}"/>
</f:facet>
<h:outputText value="#{item.descripcion}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListOfertaTitle_precioInicial}"/>
</f:facet>
<h:outputText value="#{item.precioInicial}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListOfertaTitle_precioFinal}"/>
</f:facet>
<h:outputText value="#{item.precioFinal}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{bundle.ListOfertaTitle_negocioCif}"/>
</f:facet>
<h:outputText value="#{item.negocioCif}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value=" "/>
</f:facet>
<h:commandLink action="#{ofertaController.prepareView}" value="#{bundle.ListOfertaViewLink}"/>
<h:outputText value=" "/>
<h:commandLink action="#{ofertaController.prepareEdit}" value="#{bundle.ListOfertaEditLink}"/>
<h:outputText value=" "/>
<h:commandLink action="#{ofertaController.destroy}" value="#{bundle.ListOfertaDestroyLink}"/>
<h:link outcome="/cupon/compra" value="Comprar">
<f:param name="idOferta" value="#{item.idoferta}" />
</h:link>
</h:column>
</h:dataTable>
如你所见,我有一个:
<h:link outcome="/cupon/compra" value="Comprar">
<f:param name="idOferta" value="#{item.idoferta}" />
</h:link>
我得到了下一个网址:
http://localhost:8080/agrupados/faces/cupon/compra.xhtml?idOferta=1
这个idOferta = 1我想在Managed bean中使用它,但总是,它显示为null,为什么?我之前链接的xhtml就是这个:
<h:form>
<h:panelGrid columns="2">
<h:outputLabel value="#{bundle.CreateCuponLabel_email}" for="email" />
<h:inputText id="email" value="#{cuponController.selected.email}" title="#{bundle.CreateCuponTitle_email}" required="true" requiredMessage="#{bundle.CreateCuponRequiredMessage_email}"/>
</h:panelGrid>
<br />
<h:commandLink action="#{cuponController.create}" value="#{bundle.CreateCuponSaveLink}" />
<br />
<br />
<h:commandLink action="#{cuponController.prepareList}" value="#{bundle.CreateCuponShowAllLink}" immediate="true"/>
<br />
<br />
<h:link outcome="/index" value="#{bundle.CreateCuponIndexLink}"/>
<br/>
<h:commandButton id="compra" value="COMPRAR" action="#{compra.addCompra}" />
</h:form>
ManagedBean就是addCompra:
@ManagedBean(name="compra")
@RequestScoped
public class Compra {
@EJB
private NegocioFacade negocioFacade;
@EJB
private OfertaFacade ofertaFacade;
@ManagedProperty(value = "#{param.idOferta}")
private String idOferta;
//(String)
public Compra() {
}
public void addCompra(){
System.out.println(idOferta);
FacesContext fc=FacesContext.getCurrentInstance();
String idoferta=ofer(fc);
System.out.println(idoferta);
}
public String ofer(FacesContext fc){
Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
return params.get("idOferta");
}
}
我该怎么办?,我怎么能得到这个idOferta = 1?非常感谢所有人
答案 0 :(得分:1)
您可以像这样使用f:viewParam
:
<f:metadata>
<f:viewParam name="idOferta" value="#{compra.idOferta}" />
</f:metadata>
不要忘记将idOferta的getter-setter方法添加到类compra中,并删除@ManagedProperty
注释。