2

I am using JSF 2.0 and have a simple TestButton.xhtml like below:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"     
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
  xmlns:ui="http://java.sun.com/jsf/facelets" 
  xmlns:f="http://java.sun.com/jsf/core" 
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:a4j="http://richfaces.org/a4j"
  xmlns:rich="http://richfaces.org/rich">

<h:body>
<h:form>
        <h:inputText value="#{employee.empName}"/><br/>
        <h:commandButton value="Derive" action="EmployeeTest2" >
            <f:param name="Name" value="#{employee.empName}"/>
        </h:commandButton>  
</h:form>
</h:body>
</ui:composition>

And a Managed bean which is view scoped.

@ManagedBean(name="employee", eager=true)
@ViewScoped
public class Employee implements Serializable{

String empName;
String selectedEmployeeName;


public String getSelectedEmployeeName() {
    return selectedEmployeeName;
}
public void setSelectedEmployeeName(String selectedEmployeeName) {
    this.selectedEmployeeName = selectedEmployeeName;
}

public Employee() {
    super();
}

public String getEmpName() {
    return empName;
}
public void setEmpName(String empName) {
    this.empName = empName;
}
public void manipulate(){
    System.out.println("Manipulate called...");
    System.out.println(selectedEmployeeName);
    this.selectedEmployeeName = this.selectedEmployeeName + " Jr.";
}

Now on click of button, i navigate to EmployeeTest2.xhtml which has following code :

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"  
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
  xmlns:ui="http://java.sun.com/jsf/facelets" 
  xmlns:f="http://java.sun.com/jsf/core" 
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:a4j="http://richfaces.org/a4j"
  xmlns:rich="http://richfaces.org/rich">

<f:view>
    <f:metadata>
        <f:viewParam name="Name" value="#{employee.selectedEmployeeName}"/>
        <f:event type="preRenderView" listener="#{employee.manipulate}"/>
    </f:metadata>
</f:view>

<h:body>
        <h:inputText value="#{employee.selectedEmployeeName}"/>
</h:body>   
</ui:composition>

i get correctly navigated to EmployeeTest2.xhtml but my parameter value is coming as null. I can see that manipulate method is getting called(sysouts get printed) but selectedEmployeeName is null.

Please advise as to what am i missing.

I already referred to below thread and did same but of no help.

https://stackoverflow.com/questions/20880027/passing-parameters-to-a-view-scoped bean-in-jsf

Update 1: Changed

  <h:inputText value="Chris"/>

to <h:inputText value="#{employee.empName}"/>

Put a breakpoint at setter method for selectedEmployee. Setter is not getting called which means param passing is not working.

Update 2 Please see updated testButton.xhtml

?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"    
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
  xmlns:ui="http://java.sun.com/jsf/facelets" 
  xmlns:f="http://java.sun.com/jsf/core" 
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:a4j="http://richfaces.org/a4j"
  xmlns:rich="http://richfaces.org/rich">

<h:body>
<h:form>
        <h:inputText name = "Name" value="#{employee.empName}"/><br/>
        <h:commandButton value="Submit" action="EmployeeTest2" >
            <f:param name="Name" value="#{employee.empName}"/>
        </h:commandButton>  
</h:form>
</h:body>
</ui:composition>
Community
  • 1
  • 1

1 Answers1

0

The code seems correct, in my opinion you are sending null to the next page because the value of the employee.empName is actually null.

I would debug the getEmpName() method when the TestButton.xhtml is loaded and then additionally check, after you click the button and move on to the next page whether the parameter is present in the url.

UPDATE

Ok the param is actually the value of the input. In that case add name to its definition. You form should look like this:

<h:inputText name="Name" value="#{employee.empName}"/><br/>
<h:commandButton value="Derive" action="EmployeeTest2" />

UPDATE 2

This is a workaround but you can also try this:

@PostConstruct
      public void postConstruct(){

      Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
      setSelectedEmployeeName(params.get("Name"));
}
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63