0

I'm trying to pass an object (instance of a model managed-bean) from a jsf view to a controller managed-bean through a method action from a commandButton. But i found that the object transfered is found null in the controller managed-bean, so the service in question cannot be performed this way. Here's the concerned part of the view :

<h:commandLink  action="#{employee.delete}" value="Delete account">
                <f:setPropertyActionListener target="#{empolyee.emp}" value="#{emp}" />
</h:commandLink>

And here, the part of the controller managed-bean:

@ManagedBean(name="employee")
@RequestScoped
public class EmployeeController implements Serializable {
private Employee emp = new Employee();

public Employee getEmp() {
    return emp;
}

public void setEmp(Employee emp) {
    this.emp= emp;
}

public String delete(){
    if (this.emp == null) {return "bad";}  // The execution stopped here, and the outcome corresponded is returned 
    else {    
        employImp.deleteAccount(this.emp);
        return "good";
    }
}

Why the object got null after the process ? Thanks.

Omar
  • 1,430
  • 1
  • 14
  • 31
  • [This answer](http://stackoverflow.com/questions/8116382/fsetpropertyactionlistener-sets-null-value-instead-of-intended-value) will help you. – Fritz Jul 11 '13 at 19:59
  • Where does `#{emp}` come from? – skuntsel Jul 11 '13 at 21:31
  • It's the current instance of the model managed-bean in the current view, and which has to be injected in the property of the controller managed-bean (with getter/setter). Here's from i inspired this : [link](http://stackoverflow.com/questions/2082958/can-i-pass-an-object-with-a-jsf-param-tag/17576643#17576643) – Omar Jul 11 '13 at 23:14
  • @Gamb, thanks for that answer that i've already seen before, but didn't arrive to realize it. The answer is however useful and resolved the problem. – Omar Jul 16 '13 at 17:16

1 Answers1

1

This actionListener will cause the value given by the "value" attribute to be set into the ValueExpression given by the "target" attribute.

When the listener executes, perform the following:

  • Call getValue() on the "value" ValueExpression.
  • If value of the "value" expression is null, call setValue() on the "target" ValueExpression with the null value.
  • If the value of the "value" expression is not null, call getType() on the "value" and "target" ValueExpressions to determine their property types.
  • Coerce the value of the "value" expression to the "target" expression value type following the Expression Language coercion rules. Call setValue() on the "target" ValueExpression with the resulting value.
  • If either coercion or the execution of setValue() fails throw an AbortProcessingException.

your experssion #{emp} is evaluated as null, and getEmp is not called (only setEmp with null value), but if you put #{employee.emp}, it will not be null, and getEmp is called then setEmp. One note why are you assigning value to same variable?

I suggested to put :

<f:setPropertyActionListener target="#{empolyee.emp}" value="#{empolyee.emp1}" />

emp1 is another object you created before

Safwan Hijazi
  • 2,089
  • 2
  • 17
  • 29
  • Oeps… yes… because the question was so old, I did not expect it to be marked a duplicate just now… sorry ;-) delete my comment and will delete this one to in a few hours. Maybe delete yours to – Kukeltje Jun 13 '15 at 12:13