Domanda

jsf 1.2, jboss 4.2.3 and richfaces 3.3.3

I'm trying to send the index of a as a , but it keeps returning null:

<ui:repeat id="al11" var="albumslistvalue1" value="#{AlbumDetailBean.getAlbumImagesList()}" varStatus="listimages">
  <h:form>
    <h:commandLink value="proxima" id="next" action="#{AlbumDetailBean.escreveparam()}">
      <f:param name="fotoid" value="#{listimages}" />
    </h:commandLink>
  </h:form>
</ui:repeat>

the escreveparam() method only writes the param:

public void escreveparam(){
    String fotoid = ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getParameter("fotoid");
    System.out.println("Teste:" + fotoid);
}

Why is it always null?

È stato utile?

Soluzione

You're using JSF 1.x which thus implies that you're using Facelets 1.x (as in jsf-facelets.jar file). The <ui:repeat> tag has in Facelets 1.x no varStatus attribute. It was been introduced in Facelets 2.0.

You need to look for alternate means. E.g. <c:forEach>

<c:forEach value="#{bean.albums}" var="album" varStatus="loop">
  <h:form>
    <h:commandLink id="next" value="proxima" action="#{bean.next}">
      <f:param name="id" value="#{loop.index}" />
    </h:commandLink>
  </h:form>
</c:forEach>

(please note that your initial usage of varStatus object was completely wrong, it does not give you the raw index back, but a complete object holding all iteration status details, with among others a getIndex() method, you should actually have used #{listimages.index} or something instead --provided that you were using Facelets 2.x)

or just the ID of the iterated Album object itself

<ui:repeat value="#{bean.albums}" var="album">
  <h:form>
    <h:commandLink id="next" value="proxima" action="#{bean.next}">
      <f:param name="id" value="#{album.id}" />
    </h:commandLink>
  </h:form>
</ui:repeat>

either way, just use <managed-property> in faces-config.xml with a value of #{param.id} or the ExternalContext#getRequestParameterMap() to retrieve it:

public void next() {
    String id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id");
    // ...
}

By the way, since your environment seems to support JBoss EL (as indicated by using full method names with parentheses in EL), you could also just pass the whole Album as the action method argument

<ui:repeat value="#{bean.albums}" var="album">
  <h:form>
    <h:commandLink id="next" value="proxima" action="#{bean.next(album)}" />
  </h:form>
</ui:repeat>

with

public void next(Album album) {
    // ...
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top