1

xhtml file I use a viewParam:

<?xml version="1.0" encoding="UTF-8"?>
<!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://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui"
    xmlns:f="http://java.sun.com/jsf/core">

<f:metadata>
    <f:viewParam name="actionId" value="#{editActionView.actionId}" required="true" />
</f:metadata>           
<h:body>

The backing bean looks as follows:

EditActionView.java

@ManagedBean
@ViewScoped
public class EditActionView {
    private long actionId;

    @PostConstruct
    void init() {
        System.out.println("actionId: " + getActionId());
    }


    public long getActionId() {
        return actionId;
    }

    public void setActionId(long actionId) {
        this.actionId = actionId;
    }

When I now call my application:

http://localhost:8080/aip/editAction.jsf?actionId=37

actionId is always 0. Where is my fault?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
matthias
  • 1,938
  • 23
  • 51

1 Answers1

2

The ManagedBean is constructed before the setter is called. Therefore the System.out.println(...) in the @PostConstruct method prints the default value for the actionId of type long which is 0. The setter is then called in the UPDATE_MODEL_VALUES phase. You can check this by putting another System.out.println(...) in the setter method, which should print the correct value.

Tomek
  • 494
  • 2
  • 11
  • 1
    hm... I want to initialize my backing-bean based on a GET Paramter. How would I then retrieve the value? – matthias Oct 05 '15 at 13:37
  • Here is a good answer: [http://stackoverflow.com/questions/9844526/when-to-use-fviewaction-prerenderview-versus-postconstruct](http://stackoverflow.com/questions/9844526/when-to-use-fviewaction-prerenderview-versus-postconstruct) . In summary: You can use a `` (since JSF 2.2) or a `` (JSF 2.0, 2.1) – Tomek Oct 05 '15 at 13:50