1

I can't get an action method with the f:viewAction tag to work.

Here's the jsf page:

<ui:composition xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
    <ui:repeat var="genreType" value="#{navigation.genreTypeList}">
        <f:metadata>
            <f:viewParam name="nameEn" value="#{genreType.name_en}" required="true" />
            <f:viewAction action="#{search.searchByGenreType}" />
        </f:metadata>
        <h:link value="#{genreType.name_de}" outcome="index" includeViewParams="true" /><br />
    </ui:repeat>
</ui:composition>

It produces links like this:

[...]/index.jsf;jsessionid=635562E66C7F2FA54504B53D5DAA114C?nameEn=fiction

And here's the bean:

@ManagedBean
@RequestScoped
public class Search implements Serializable {

    private static final long serialVersionUID = -5193732222381183093L;

    private String nameEn;

    public String getNameEn() {
        return this.nameEn;
    }

    public void setNameEn(String nameEn) {
        this.nameEn = nameEn;
    }

    // action methods
    public String searchByGenreType() {
        System.out.println("searchByGenreType");
        return "index";
    }
}

I'm using JSF 2.2.5 with Tomcat 7.0.42, IDE is Eclipse Kepler (4.3.1). I've tried different variations (@PostConstruct in bean, explicit navigation in faces-config.xml, old and new namespaces).

There should be no problem with namespaces since this is fixed since JSF 2.2.5.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • What happens if you use the `f:metadata` outside of the `ui:repeat`? – mabi Feb 12 '14 at 13:57
  • 1
    The code is in first place not making any sense. `` huh? What exactly is the concrete functional requirement? Perhaps you're confusing `` with ``? – BalusC Feb 12 '14 at 14:28
  • @BalusC: The jsf page is included in a template via ui:include. Now I know it doesn't work that way. The idea is iterating through genreTypeList so I get a list of links. Every link is supposed to be able to call the action method. –  Feb 12 '14 at 15:42
  • Okay, I posted an answer. – BalusC Feb 12 '14 at 15:49

1 Answers1

2

This code isn't making any sense. You seem to be confusing <f:viewParam> with <f:param>. You need <f:param> to add HTTP request parameters to links. The <f:viewParam> is to be used to set incoming HTTP request parameters as bean properties.

Given the concrete functional requirement of having a list of links with parameters which in turn should on the target page set the parameter as a bean property and invoke a bean action, here's how you should be implementing it:

The source page with the list of links:

<ui:repeat var="genreType" value="#{navigation.genreTypeList}">
    <h:link value="#{genreType.name_de}" outcome="index">
        <f:param name="nameEn" value="#{genreType.name_en}" />
    </h:link>
    <br />
</ui:repeat>

In the target page, apparently index.xhtml, put this somewhere in top, right before <h:head>:

<f:metadata>
    <f:viewParam name="nameEn" value="#{search.nameEn}" required="true" />
    <f:viewAction action="#{search.searchByGenreType}" />
</f:metadata>

Inside the searchByGenreType() method you can just access that nameEn property directly.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • Thanks. Now it works. The problem was indeed the confusion about the mentioned tags and about the structure of the templates I used. –  Feb 12 '14 at 16:16