JSF-如何在立即执行操作后如何在ui:repeat中保留瞬态值 [英] JSF - How can I retain the transient values within a ui:repeat after an immediate action

查看:42
本文介绍了JSF-如何在立即执行操作后如何在ui:repeat中保留瞬态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了说明我的疑问,我根据自己的项目创建了一个非常简单的示例.这是一种使用电话号码列表注册人的方法.

I created a very simple example based on my project in order to illustrate my doubt. Just a way to register a person with a list of telephone numbers.

private String name;
private List<Phone> phoneList;
// Getters and Setters

@PostConstruct
private void init() {
    phoneList = new ArrayList<>();
}

public static class Phone implements Serializable {

    private String number;
    // Getters and Setters

    @Override
    public String toString() {
        return number != null ? number : "null";
    }
}

public void add() {
    phoneList.add(new Phone());
}

public void save() {
    System.out.println("Name: " + name + "; " + phoneList.toString());
}

index.xhtml

<h:form>
    <h:inputText value="#{mainController.name}" required="true" />
    <ui:repeat var="phone" value="#{mainController.phoneList}" varStatus="status">
        <h:inputText value="#{phone.number}" required="true" />
    </ui:repeat>
    <h:commandButton action="#{mainController.add()}" value="Add Phone" immediate="true" />
    <h:commandButton action="#{mainController.save()}" value="Save" />
</h:form>

在我的示例中,请注意,必须添加所有已添加的电话字段(必填= true).

In my example, note that all phone fields that are added MUST be filled in (required = true).

问题是:当我键入名称并单击添加(以添加电话)时,该字段的值将保持不变.但是,当我键入第一部电话并单击添加时,电话的价值不会保留.组件ui:repeat中的所有字段都会发生这种情况.

The problem is: when I type name and click add (to add a phone) the value of the field is maintained. But when I type a first phone and click add, the phone's value is not maintained. This occurs for all fields within the component ui:repeat.

是否有一种方法可以像在 name 字段中那样在立即请求后的时间内保留输入值?

Is there a way to preserve the input values within a after an immediate request, as with the name field?

附加说明:我注意到的其他奇怪行为是,当添加至少两个电话字段时,让第一个空白并填写第二个字段,然后保存表格.验证失败后(由于电话空白),单击添加将使第二个电话的值填充所有字段.

Extra note: Other strange behavior I noticed is when add at least two phone fields, let the first blank and fills the second, and saves the form. After a failed validation (due to phone blank), click add will make all fields are filled with the value of the second phone.

Wildfly 9.0.2,JSF Api(Jboss)2.2.12

Wildfly 9.0.2, JSF Api (Jboss) 2.2.12

推荐答案

感谢@BalusC OmniFaces 库有两个可以在这种情况下使用的标记处理程序.在两种情况下,如果验证失败,将保留输入值.请注意,h:commandButton应该与<h:commandButton immediate="false" />一起使用.

Thanks to @BalusC comment. The OmniFaces library has two taghandlers that can be used in this case. In both cases input values will be preserved in case of validation failure. Note that h:commandButton should be with <h:commandButton immediate="false" />.

在这种情况下,所有验证失败(包括转换器失败)将被忽略.请注意,必须将h:form更改为o:form.同样,仍将显示失败消息,可以通过在呈现的属性中放置适当的条件来解决此问题.文件将如下所示:

In this case all validation failures will be ignored (including converter failures). Note that the h:form have to be changed to o:form. Also, the failures messages will still be displayed, which can be solved putting a proper condition in the rendered attribute. The files will look like this:

<o:form>
    <h:inputText value="#{mainController.name}" required="true" />
    <ui:repeat var="phone" value="#{mainController.phoneList}" varStatus="status">
        <h:inputText value="#{phone.number}" required="true" />
    </ui:repeat>
    <h:commandButton action="#{mainController.add()}" value="Add Phone">
        <o:ignoreValidationFailed />
    </h:commandButton>
    <h:commandButton action="#{mainController.save()}" value="Save" />
</o:form>
<h:messages rendered="#{facesContext.validationFailed}" />

skipValidators

在这种情况下,仅验证失败将被忽略(转换器仍将运行).除转换器外,将不显示故障消息.请注意,此标记处理程序仅从2.3版本开始可用.文件将如下所示:

skipValidators

In this case only the validation failures will be ignored (the converters will still run). The failures messages will not be displayed, except for the converters. Note that this taghandler is only available since the 2.3 version. The files will look like this:

<h:form>
    <h:inputText value="#{mainController.name}" required="true" />
    <ui:repeat var="phone" value="#{mainController.phoneList}" varStatus="status">
        <h:inputText value="#{phone.number}" required="true" />
    </ui:repeat>
    <h:commandButton action="#{mainController.add()}" value="Add Phone">
        <o:skipValidators />
    </h:commandButton>
    <h:commandButton action="#{mainController.save()}" value="Save" />
</h:form>

这篇关于JSF-如何在立即执行操作后如何在ui:repeat中保留瞬态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆