0

I just started learning jsf and I encountered a basic problem. I displayed a list of objects in dataTable and created form for each row to call a function with right parameter:

<h:dataTable value="#{test.getMyItems()}" var="o"
            styleClass="order-table"
            headerClass="order-table-header"
            rowClasses="order-table-odd-row,order-table-even-row">

<h:column>
    <f:facet name="header">Content</f:facet>
    #{o.content}
</h:column>
<h:column>
<h:form>
    <h:commandLink action="#{test.edit}" value="edit">
    <f:param name="id" value="#{o.id}" />
    </h:commandLink>
</h:form>
</h:column>
</h:dataTable>

And now I wanted to redirect user to edit page whit a form which has inputs set to proper values. So in function edit I set values to the variables:

public String edit() {
    this.name = "test1";
    this.description = "test2";

    return "/edit.xhtml?faces-redirect=true";
}

And in edit view I wanted to display them:

<h:form>
            <h:outputLabel value="name:" />
            <h:inputText value="#{test.name}" />

            <h:outputLabel value="description" />
            <h:inputText value="#{test.description}" />

            <h:commandButton value="Add" action="#{edit.update()}"/>
 </h:form>

but inputs are empty. Why?

And Test class:

@ManagedBean (name = "test")
@ViewScoped
public class Test {
    private String name;
    private String description;

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return this.description;
}

public void setDescription(String description) {
    this.description = description;
}

public String edit() {
    this.name = "test1";
    this.description = "test2";

    return "/edit.xhtml?faces-redirect=true";
}

 public List<Item> getMyItems() {
    List<Item> items = new ArrayList<Item>();
    Item i = new Item("1", "g", "f");
    items.add(i);

    return items;
} 

public void update() {
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

1 Answers1

0

They are empty because you're sending a redirect to a new view. A view scoped bean lives as long as the current view lives. A redirect to a new view destroys this.

In this specific construct, you need a <h:link>, not a <h:commandLink>.

Replace

<h:form>
    <h:commandLink action="#{test.edit}" value="edit">
    <f:param name="id" value="#{o.id}" />
    </h:commandLink>
</h:form>

by

<h:link value="edit" outcome="edit">
    <f:param name="id" value="#{o.id}" />
</h:link>

with in edit.xhtml

<f:metadata>
    <f:viewParam name="id" value="#{edit.id}" />
    <f:viewAction action="#{edit.init}" />
</f:metadata>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the answer but changing my comamndLink to link means there is no call to the edit function in Test class, right? I set proper values to my variables in edit function so I can't simply omit this step. – tom.michalek May 18 '15 at 06:13
  • You just set those values based on `#{o.id}` during construction of the bean associated with `edit.xhtml`. Click the first "See also" link for an extensive example. Do note that I never post "See also" links for pure decoration. They all really go in depth in the background of the current answer. Follow them before asking contra questions. – BalusC May 18 '15 at 06:14