3 回答

TA贡献1799条经验 获得超8个赞
如果将值绑定到基元而不是包装器表示形式,则会发生这种情况。图元int始终默认为0,而图元double始终默认为0.0。您想改用Integer或Double(或BigDecimal)。
例如:
public class Bean {
private Integer number;
// ...
}
然后,在处理表单提交时还要考虑两件事。首先,您需要指示JSF将空字符串提交的值解释为null,否则EL仍会将空字符串强制为0或0.0。这可以通过以下上下文参数完成web.xml:
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<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

TA贡献1876条经验 获得超5个赞
我不同意先前给出的答案,但我认为您可能正在寻找
<h:inputText id="number" binding="#{controller.inputBox}" value="#{controller.number}" >
<f:event type="preRenderComponent" listener="#{controller.preRenderInput}" />
</h:inputText>
在控制器内部
private double number; // getters and setters
private UIInput inputBox; // getters and setters
public void preRenderInput() {
// if it is the first request
if(!FacesContext.getCurrentInstance().isPostback()) {
inputBox.setValue("");
}
}
JSF 2.0以上

TA贡献1872条经验 获得超3个赞
在您的XHTML代码中,假设您的输入字段为:
<h:inputText id="newname" value="#{youAction.newName}"
style="width:130px" styleClass="form">
</h:inputText>
在您的Managed Bean Action Class中,将此字段映射为:
@ManagedBean(name="yourAction")
public class YourAction {
private String newName;
...
// Add Getter & Setter Methods
...
}
通过这样做,你不会得到0或0.00在你输入字段的默认值。
添加回答
举报