简体   繁体   中英

f:viewAction does not invoke action method

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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM