Unable to access GET parameters in JSF backing bean

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.

Answers


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

Need Your Help

Problems with UIImagePickerController cancel button not working

objective-c ios uiimagepickercontroller

I have an universal app that allows to select an image from the device photo library for later manipulation, the code works fine on the iPad but nothing happens on the iPhone, not even the cancel b...

How to configure Cached Exchange mode for Outlook using C#?

c# .net outlook ms-office office-interop

I am working on a tool that needs to configure the Cached Exchange mode for outlook. I tried meddling with the registry but it seems like its not a good way to go about it, since in Outlook 2010 it...