【发布时间】: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