【问题标题】:passing values from controller to jsf view将值从控制器传递到 jsf 视图
【发布时间】:2015-05-18 04:37:05
【问题描述】:

刚开始学习jsf,遇到一个基本问题。我在 dataTable 中显示了一个对象列表,并为每一行创建了表单以调用具有正确参数的函数:

<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>

现在我想将用户重定向到编辑页面,该页面的输入设置为正确的值。所以在函数编辑中,我为变量设置了值:

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

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

我想在编辑视图中显示它们:

<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>

但输入为空。为什么?

和测试类:

@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() {
}

【问题讨论】:

  • 如果你展示了你的测试课会有所帮助。您的名称和描述字段是否正确命名了 getter 和 setter?
  • @hooda 我添加了测试类实现。

标签: jsf controller master-detail


【解决方案1】:

它们是空的,因为您将重定向发送到新视图。视图范围 bean 的存在时间与当前视图的存在时间一样长。重定向到新视图会破坏这一点。

在这个特定的构造中,您需要&lt;h:link&gt;,而不是&lt;h:commandLink&gt;

替换

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

通过

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

edit.xhtml

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

另见:

【讨论】:

  • 感谢您的回答,但将我的 comamndLink 更改为链接意味着在测试类中没有调用编辑功能,对吧?我在编辑函数中为我的变量设置了适当的值,所以我不能简单地省略这一步。
  • 您只需在构造与edit.xhtml 关联的bean 期间根据#{o.id} 设置这些值。单击第一个“另见”链接可查看详细示例。请注意,我从不发布纯装饰的“另见”链接。他们都在当前答案的背景下真正深入。在提出相反的问题之前先关注他们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多