Primefaces动态工具提示

时间:2018-10-23 21:41:39

标签: primefaces

我正在尝试所谓的“动态工具提示”-我有一个带有按钮的inputNumber字段,并且在按钮上单击时,如果值不是正值,我想显示工具提示。

页面

<?xml version="1.0" encoding="UTF-8"?>
<!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:f="http://xmlns.jcp.org/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui"
    xmlns:sec="http://www.springframework.org/security/tags" xmlns:ui="http://java.sun.com/jsf/facelets">

<h:body>

    <p:growl id="msgs" showDetail="true" />

    <h:form>
        <p:outputLabel value="Enter positive integer" />
        <p:inputNumber id="val" value="#{dynamicTooltipView.val}" decimalPlaces="0" />
        <p:tooltip id="tooltip" for="val" value="Test" />
        <p:commandButton value="Validate" actionListener="#{dynamicTooltipView.validate}" update="val" process="val"/>
    </h:form>

</h:body>
</html>

Bean

package com.codenotfound.primefaces.model;

import javax.faces.view.ViewScoped;
import javax.inject.Named;

@Named
@ViewScoped
public class DynamicTooltipView {

    Integer val;
    boolean isValid;

    public void validate() {
        isValid = val != null && val > 0;
    }

    public Integer getVal() {
        return val;
    }

    public void setVal(Integer val) {
        this.val = val;
    }

    public boolean isValid() {
        return isValid;
    }

    public void setValid(boolean isValid) {
        this.isValid = isValid;
    }

}

问题是,当我单击“验证”按钮时,工具提示丢失了,并且我不明白为什么以及如何解决此问题...

1 个答案:

答案 0 :(得分:0)

问题似乎出现在process="val"中,当我指定该问题时,不再调用我的actionListener(#{dynamicTooltipView.validate})(但我想了解原因)...

此外,update="val tooltip"也不起作用,我必须指定update="@form"使其起作用,但这并不是我想要的...

我最终得到了这个解决方案(我使用了p:fragment,但未更新按钮):

<?xml version="1.0" encoding="UTF-8"?>
<!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:f="http://xmlns.jcp.org/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui"
    xmlns:sec="http://www.springframework.org/security/tags" xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head>
    <style>
        span.invalid>input {
            border-color: orange;
        }
    </style>
</h:head>

<h:body>

    <p:growl id="msgs"/>

    <h:form>
        <p:fragment autoUpdate="true">
            <p:outputLabel value="Enter positive integer" />
            <p:inputNumber id="val" value="#{dynamicTooltipView.val}" decimalPlaces="0" styleClass="#{dynamicTooltipView.isValid() ? '' : 'invalid'}"/>
            <p:tooltip id="tooltip" for="val" value="Entered value is not positive integer ;-)" rendered="#{dynamicTooltipView.isValid() == false}" />
            <p:commandButton value="Validate" actionListener="#{dynamicTooltipView.validate}" />
            <p:outputLabel value="#{dynamicTooltipView.isValid()}" />
        </p:fragment>
    </h:form>

</h:body>
</html>

p:outputLabel仅用于调试...