Skip to content
javahacks.dev
JavaServer Faces

JSF Value Not Updated in the Bean and How to Fix It

Fix JSF input values that do not reach the bean: missing setter, request scope loss, validation failure, immediate, AJAX execute and wrong binding.

8 min read
Summarize in AI
Flat illustration of a browser page built from server-side components, with the OpenJDK, Spring, Thymeleaf and Vaadin logos.

The user types a new email address, clicks Save, the page reloads with a success message and the old address. The form submitted, the request arrived, no exception appeared in the log and the field is exactly where it was. In JSF the value of an input component reaches your bean in a specific phase, Update Model Values, and a failure anywhere before that phase drops the value without comment. This post lists the seven reasons a bean property does not change, in the order they occur in practice, each with how to confirm it and the fix. The examples run on Jakarta Faces 4.1 with CDI beans, and the lifecycle phases referred to are laid out in the JSF lifecycle explained.

The page used throughout is a single field and a button:

src/main/webapp/profile.xhtmlXML
<h:form>
    <h:messages />
    <h:inputText id="email" value="#{profileBean.email}" />
    <h:commandButton value="Save" action="#{profileBean.save}" />
</h:form>
src/main/java/dev/javahacks/web/ProfileBean.javajava
@Named
@ViewScoped
public class ProfileBean implements Serializable {

    private String email;

    public void setEmail(String email) {
        System.out.println("setEmail(" + email + ")");
        this.email = email;
    }

    public String getEmail() { return email; }

    public String save() {
        System.out.println("save with " + email);
        return null;
    }
}

When it works, the console prints setEmail([email protected]) and then save with [email protected], in that order, because the model is updated before the action runs. If setEmail never prints, one of the causes below applies. If it prints and the value is still wrong afterwards, go to the last section.

1. The setter is missing or misnamed

EL resolves #{profileBean.email} to getEmail() for reading and setEmail(String) for writing, following the JavaBeans naming rule. A getter without a setter makes the property read-only; submitting it throws PropertyNotWritableException, which Faces reports as a message or an error page. A setter with the wrong name or parameter type does not match.

java
public String getEmail() { return email; }
public void setEMail(String email) { this.email = email; }    // wrong: the property is "EMail"

Check by looking for the message in the log: Cannot find property 'email' with setter is not always printed, so the dependable test is the println in the setter. Fix the name. Boolean properties follow isActive() and setActive(boolean), and a field named fooBar has accessors getFooBar and setFooBar. A Lombok @Getter and @Setter on the class generates correct ones, but only if the annotation processor runs; check that the setter is in the compiled class with javap. How EL maps a path to accessors is in JSF expression language and value binding.

2. Conversion or validation failed

This is the most common cause, and the same one as for a silent button. The model is updated only if the component passed conversion and validation. If either failed, Faces skips straight to rendering, no setter is called and the field redisplays the submitted text with a message. With no h:message on the page, nothing is visible. The required="true" attribute, a <f:validateLength>, a Bean Validation annotation such as @Email on the bean property and a number or date converter are all candidates.

Text
email: Validation Error: Value is required.

Add <h:messages /> and click again. The detail of the validators and converters is in JSF validation, converters and messages. One field failing blocks every field in the form, including those that are valid, because the whole phase is skipped: a user who fills in three fields and trips a validator on a fourth sees the three revert. That is the lifecycle, not a bug, and it is why <h:messages> belongs on every page.

3. Request scope forgot it

A @RequestScoped bean lives for one request. The value you set during the postback is gone at the next request, and the next request builds a new bean from scratch. A page that submits, then redirects, then shows the property, reads a brand-new bean with the default value.

java
@Named
@RequestScoped          // new instance for every request
public class ProfileBean { /* ... */ }

The setter does run, and the value is lost afterwards. Prove it by printing this.hashCode() in the setter and in the getter on the following request; two different numbers mean two different beans. For a form that stays on one view across submits, use @ViewScoped (Jakarta Faces), which survives postbacks to the same view and is discarded when the user navigates away. For data that must survive a redirect, use a flash message or @SessionScoped for the minimum. The scopes are compared in JSF bean scopes explained. Make sure the @ViewScoped import is jakarta.faces.view.ViewScoped, with @Named from CDI; a mix with the old javax.faces.bean annotations produces a bean the container treats differently, see JSF managed bean versus CDI beans.

4. AJAX did not send the field

With <f:ajax>, only the components listed in execute go through the lifecycle. A button whose AJAX call has execute="@this" processes just itself, so the input beside it is not decoded, not validated and not set.

XML
<!-- Wrong for a form: only the button is processed -->
<h:commandButton value="Save" action="#{profileBean.save}">
    <f:ajax execute="@this" render="status" />
</h:commandButton>

<!-- Right: include the inputs -->
<h:commandButton value="Save" action="#{profileBean.save}">
    <f:ajax execute="@form" render="status" />
</h:commandButton>

@form sends every input in the surrounding form; a list such as execute="email name" sends specific ones by id. The reverse problem is render: the value reached the bean, but the part of the page that displays it was not in the list to re-render, so the screen looks stale. Open the browser's network tab and read the request body: the field must be present as a form parameter. The mechanics of partial processing and rendering are in partial page updates with f:ajax.

5. The browser did not send it

HTML does not submit disabled inputs, and an input marked readonly through the component attribute may or may not be decoded depending on the component. A field hidden with CSS is still sent; a field removed by rendered="false" is not in the page at all, and Faces will not set a value for a component it did not render on the postback. If a field is rendered conditionally and the condition is false at postback time, the value is ignored. Check the request in the network tab, then check rendered and disabled conditions; their instability across requests is described in the commandButton checklist. An input outside the form that holds the button is never submitted either: two separate h:form elements are two independent submissions.

6. A binding or EL problem

If the expression cannot be resolved, the failure is different and louder, usually Target Unreachable, identifier 'profileBean' resolved to null. That means the bean does not exist under that name: a missing @Named, a wrong name, or a bean scanned by a container that is not seeing the class. The diagnosis is in Target Unreachable, identifier resolved to null. A subtler version: a value expression that ends in a method call or a read-only expression (#{profileBean.user.name} where user is null) cannot be written, since EL will not create the intermediate object. Make sure every link in the path exists before the postback, by initializing it in @PostConstruct:

java
@PostConstruct
void init() {
    user = new User();
}

Keep value expressions short and writeable: a property of the bean, or of an object the bean owns. Chains through collection lookups (#{map[key].field}) are legal and fragile.

7. The setter runs, but something resets it

If setEmail prints and the page still shows the old value, the value was set and then overwritten. The usual culprits:

  • The action reloads the entity. save() calls service.find(id) and assigns the result to the bean's field, replacing the object that held the new value.
  • A getter that recomputes. getEmail() reads from the database each time instead of returning the field, so every render shows the stored value:
java
public String getEmail() {
    return userRepository.findEmail(userId);    // ignores what the setter stored
}
  • **A @PostConstruct or a preRenderView listener** that re-initializes the bean on each request in a request-scoped bean.
  • Two beans of the same name, one shadowing the other, or a cached copy in session scope reading old data.
  • A converter that normalizes. The value you typed is converted on its way in, so the stored string differs from the typed one (trimmed, lower-cased).

Find which by printing at the getter and in save() and reading the sequence of calls in order. The getter is called several times per request by design, so never put queries in it; load once in @PostConstruct or in the action.

A debugging aid

A phase listener that logs when the model update phase starts and ends, with the number of queued messages, tells you in one run where the lifecycle ended, as shown in the previous checklist. For a quicker view, add this to the page during debugging:

XML
<ui:debug hotkey="x" rendered="#{facesContext.application.projectStage == 'Development'}" />

Press Ctrl+Shift+X on the page to see the component tree, the request parameters, the scoped variables and the state of each component, which answers "did the parameter arrive, and what does the component hold?" in one panel. Setting jakarta.faces.PROJECT_STAGE to Development also makes Faces print more helpful warnings. The specification of the phases is at Jakarta Faces 4.0, and EL is at Jakarta Expression Language.

Where this does not apply

If the value is set correctly in the bean and the page shows something else, the issue is on the render side: a stale AJAX render list, a browser cache or a component with its own submitted value. If the bean is correct after the request but wrong after a restart, you are looking at persistence, not JSF. And for PrimeFaces components, such as a calendar or an autocomplete, the same lifecycle applies, but the component can have its own converter and itemValue rules, so check those first.

Also inDebugging
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.