验证失败后Primefaces JSF更新不起作用 [英] Primefaces JSF update after validation failed doesn't work

查看:114
本文介绍了验证失败后Primefaces JSF更新不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在验证<p:inputText>并更新其内容时遇到问题.

I have a problem with the validation of a <p:inputText> and updating its content.

基本上,当inputText验证失败时,它再也不会更新.

Basically when the inputText validation fails, it never gets updated again.

这里有一个简单的例子可以澄清:

Here's a simple example to clarify:

Facelet:

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
    <h:head>
    </h:head>
    <body>
    <h1>Test</h1>

        <h:form id="list" prependId="false">
            <ul>
                <li>Element 1&#160;
                    <p:commandLink action="#{Test.assignElement}" update="detail_value">
                        <f:setPropertyActionListener target="#{Test.currentElement}" value="1" />
                        Assign
                    </p:commandLink>
                </li>
                <li>Element 2&#160;
                    <p:commandLink action="#{Test.assignElement}" update="detail_value">
                        <f:setPropertyActionListener target="#{Test.currentElement}" value="2" />
                        Assign
                    </p:commandLink>
                </li>
            </ul>
        </h:form>

        <h:form id="detail" prependId="false">
            <p:inputText value="#{Test.element}" id="detail_value" required="true" styleClass="#{Faces.messagesFor['detail_value'] ? 'border:1px solid red' : ''}">
                <p:ajax event="blur" update="detail_value"></p:ajax>
            </p:inputText>
        </h:form>
    </body>
</html>

测试bean:

package com.easydevel.test;

public class Test {

    private String currentElement;
    private String element;

    public String getCurrentElement() {
        return currentElement;
    }

    public void setCurrentElement(String currentElement) {
        this.currentElement = currentElement;
    }

    public String getElement() {
        return element;
    }

    public void setElement(String element) {
        this.element = element;
    }

    public String assignElement(){
        setElement(getCurrentElement());
        return "";
    }

}

如果单击元素"下方的命令链接,则输入字段将更新,但是如果验证失败(只需将输入文本保留为空白,然后单击页面的任何其他部分),则输入的边框变成红色.此后,单击上面的命令链接,它将永远不会再更新.

If you click on the commandLinks below the "Element"s the input field gets updated, but when a validation fails (simply leave the input text blank, and click on any other part of the page), the border of the input turns red. After that it never gets updated again when clicking on the above mentioned commandLinks.

有什么想法吗?

推荐答案

Arjan tijms 答案会起作用,但是我发现的最佳解决方案是:

Arjan tijms answer will works, however the best solutions I found are:

  1. 使用 Omnifaces解决方案 因此,您不需要自己实现侦听器,而是只需要一行简单的代码即可.

  1. Use Omnifaces Solution So, instead of implementing the listener your self all what you need is just one line of simple code.

<h:commandButton value="Update" action="#{bean.updateOtherInputs}">
<f:ajax execute="currentInputs" render="otherInputs" />
<f:actionListener type="org.omnifaces.eventlistener.ResetInputAjaxActionListener" />
</h:commandButton>

  • 如果您使用的是Primefaces,则可以使用 resetInput组件:

    <p:commandButton value="Reset Non-Ajax" actionListener="#{resetInputView.reset}"
    immediate="true" ajax="false" style="margin-right:20px;">
    <p:resetInput target="panel" />
    </p:commandButton>  
    

  • 这篇关于验证失败后Primefaces JSF更新不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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