Question

I am unable to access GET parameters in a backing bean. The example.xhtml file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

<f:metadata>
   <f:viewParam name="id" value="#{myBackingBean.unitId}" />
   <f:viewAction action="#{myBackingBean.findUnits}" />
</f:metadata>

<head>
<title>Example Title</title>
</head>
<body>
I found #{myBackingBean.units.size()} units.
</body>
</html>

My understanding is that when I GET example.jsf?id=3 then JSF calls myBackingBean.setUnitId(3). Correct?

Here is MyBackingBean:

@Model
public class MyBackingBean {

    private static Logger logger = Logger.getLogger(MyBackingBean.class
            .getName());

    //@ManagedProperty("#{param.id}")
    private Long unitId;

    @Inject
    private IExampleEJB myExampleEjb;

    private List<Unit> units;

    @PostConstruct
    public void postConstruct() {
        Map<String, String> mymap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();

        for (String k:mymap.keySet()) {
            logger.info(String.format("%s %s", k,mymap.get(k)));
        }
    }

    public void findUnits() {   
        logger.info(String.format("MyBackingBean findUnits %d", unitId));
        units = myExampleEjb.findUnits();
    }

    public List<Unit> getUnits() {
        return units;
    }

    public void setUnits(List<Unit> units) {
        this.units = units;
    }

    public Long getUnitId() {
        return unitId;
    }

    public void setUnitId(Long unitId) {
        this.unitId = unitId;
    }
}

The id parameter is in the parameter request map in postConstruct().

In my case, setUnitId() is never called, nor is findUnits().

In a different but related question, the @ManagedProperty (currently commented out) does not work either.

Was it helpful?

Solution

Given the xmlns.jcp.org namespace, you're using JSF 2.2. The early Mojarra 2.2.0 and 2.2.1 versions have a known bug which causes the <f:viewParam> to not work.

As per the comments you're using GlassFish 4.0. If you didn't upgrade its bundled JSF implementation and thus are using the early Mojarra version 2.2.0, then you're definitely facing this bug and you need to upgrade.

You can get the JAR from http://javaserverfaces.java.net. All you need to do is to replace javax.faces.jar file in GlassFish's /modules folder with the newer version.

See also

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top