Question

I'm having problems updating a bean variable from an xhtml page with JSF. I am able to update the Bean variable with a custom value but I want the value to be read from a variable which is defined in the xhtml page itself.

The relevant part of the xhtml page looks like this:

<?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:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:p="http://primefaces.org/ui">

<h:body>
    <ui:repeat value="#{bean.variable}" var="results">
        <c:set target="#{bean}" property="otherVariable" value="results.name" />

        Testing if the bean-variable could be read in the loop: #{results.email}

    </ui:repeat>
</h:body>
</html>

In the Bean class I am printing the otherVariable to stdout. It is null everytime. When I replace the value in the Tag with let's say "5" the value is being set correctly in the Bean-variable.

So how can I update the Bean-variable from the xhtml page?

Was it helpful?

Solution

You have two problems here:

  1. You're trying to set the value in your c:set tag without using the proper EL: "#{results.name}"

  2. c:set tags are evaluated too early within the faces lifecycle to be available within the contents of a ui:repeat tag

In these cases, I would typically just pass this value in as a parameter to the backing bean method that needs it. For example: #{results.doSomething(results.name)}

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