【问题标题】:Restrict number input to two fixed digits after decimal point将数字输入限制为小数点后两位固定数字
【发布时间】:2015-06-29 06:34:48
【问题描述】:

我们的项目使用 PrimeFaces 5.1。我需要限制用户输入的数字类型。例如,用户只能输入 123.12(小数点后两位)。 我尝试使用<p:inputMask>,但问题是它定义的掩码是固定长度的。用户必须准确输入在<p:inputMask>mask 属性中定义的长度。 PrimeFaces中允许定义小数点后多少位并且还可以控制长度的正确方法是什么?

更新:

我尝试使用 inputNumber 组件创建一个简单的页面。但不知何故,在 inputNumber 中输入的任何值都将被视为 null,因为它无法在页面级别通过所需的验证。

下面是xhtml。

<?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:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui"
      xmlns:pe="http://primefaces.org/ui/extensions">

    <h:head>
        <title>InputNumberTestingPage</title>
    </h:head>

    <h:body>
        <h:form id="form">
            <p:panel id="panel" header="Form" style="margin-bottom:10px;">
                <p:messages id="messages" />
                <h:panelGrid columns="3" cellpadding="5">
                    <p:outputLabel for="Name" value="Name: " />
                    <p:inputText id="Name" value="#{inputNumberTesting.name}" required="true" label="Name">
                        <f:validateLength minimum="2" />
                    </p:inputText>
                    <p:message for="Name" />

                    <p:outputLabel for="Salary" value="Salary:" />
                    <pe:inputNumber id="Salary" value="#{inputNumberTesting.salary}" 
                                    required="true" label="Salary" decimalPlaces="2" maxValue="9999"/>
                    <p:message for="Salary" />
                </h:panelGrid>
            </p:panel>
            <p:commandButton value="Submit" update="panel" actionListener="#{inputNumberTesting.save}" style="margin-right:20px;" />
        </h:form>
    </h:body>

</html>

支持 Bean。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "inputNumberTesting")
@ViewScoped
public class InputNumberTesting {
    private Object name;

    private Object salary;

    public Object getName() {
        return name;
    }

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

    public Object getSalary() {
        return salary;
    }

    public void setSalary(Object salary) {
        this.salary = salary;
    }

    public void save() {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Name: "+getName()+"Salary: "+getSalary()));
    }
}

【问题讨论】:

  • 您可以查看 PrimeFaces Extension 中的 pe:inputNumber。它对我很有效。
  • @ForguesR 我尝试使用 inputNumber 但不知何故不起作用。该值始终为空
  • 抱歉,我在您发布的内容中找不到任何问题。

标签: jsf jsf-2 primefaces


【解决方案1】:

您可以在 primefaces 扩展库中使用 InputNumber 组件。首先你需要添加它的依赖然后你可以使用扩展命名空间和用法应该像

xmlns:pe="http://primefaces.org/ui/extensions"


<pe:inputNumber id="Input1" value="#{inputNumberController.input1}"/>

您可以自定义最小最大数字、小数位、货币符号以及组件中存在的更多自定义选项。

【讨论】:

  • 我已经更新了问题区域。不太确定我做错了什么,因为在该组件中似乎没有任何价值。有什么想法吗?
  • 不确定出了什么问题,但尝试将 commandButton 中的进程属性设置为 process="@this panel" 并尝试在支持 bean 中使用 Double/Float 或 BigDecimal 作为目标值。跨度>
  • 感谢伙伴,在我更改为 Double 后它可以工作。但有趣的是只接受 Double 而不是 Object。
猜你喜欢
  • 1970-01-01
  • 2022-01-11
  • 2017-04-01
  • 2018-11-16
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2018-04-11
  • 2023-01-08
相关资源
最近更新 更多