猿问

如何使数字输入区域最初为空而不是0或0.00?

我有一个应该输入数字的地方。我希望它显示为空。但是,当我运行程序时,默认值为0或0.00。如何将其清空?



翻阅古今
浏览 636回答 3
3回答

守着星空守着你

如果将值绑定到基元而不是包装器表示形式,则会发生这种情况。图元int始终默认为0,而图元double始终默认为0.0。您想改用Integer或Double(或BigDecimal)。例如:public class Bean {&nbsp; &nbsp; private Integer number;&nbsp; &nbsp; // ...}然后,在处理表单提交时还要考虑两件事。首先,您需要指示JSF将空字符串提交的值解释为null,否则EL仍会将空字符串强制为0或0.0。这可以通过以下上下文参数完成web.xml:<context-param>&nbsp; &nbsp; <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>&nbsp; &nbsp; <param-value>true</param-value></context-param>其次,如果你使用的是Tomcat / JBoss的或使用Apache EL解析器盖下的任何服务器,那么你需要指示它不强迫null到0或0.0在案件Number类型由以下VM参数(它unintuitively处理Number类型,如果它们是原始元素):-Dorg.apache.el.parser.COERCE_TO_ZERO=false

慕运维8079593

我不同意先前给出的答案,但我认为您可能正在寻找<h:inputText id="number" binding="#{controller.inputBox}" value="#{controller.number}" >&nbsp; &nbsp; &nbsp;<f:event type="preRenderComponent" listener="#{controller.preRenderInput}" /></h:inputText>在控制器内部private double number; // getters and settersprivate UIInput inputBox; // getters and setterspublic void preRenderInput() {&nbsp; &nbsp; &nbsp; &nbsp; // if it is the first request&nbsp; &nbsp; &nbsp; &nbsp; if(!FacesContext.getCurrentInstance().isPostback()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputBox.setValue("");&nbsp; &nbsp; &nbsp; &nbsp; }}JSF 2.0以上

守着一只汪

在您的XHTML代码中,假设您的输入字段为:<h:inputText id="newname" value="#{youAction.newName}"&nbsp; &nbsp; &nbsp; &nbsp; style="width:130px" styleClass="form"></h:inputText>在您的Managed Bean Action Class中,将此字段映射为:@ManagedBean(name="yourAction")public class YourAction {&nbsp; &nbsp;private String newName;&nbsp; &nbsp;...&nbsp; &nbsp;// Add Getter & Setter Methods&nbsp; &nbsp;...&nbsp;}通过这样做,你不会得到0或0.00在你输入字段的默认值。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答