gpt4 book ai didi

jsf - 整数获取默认值 0,在 Java 中它需要为 NULL

转载 作者:行者123 更新时间:2023-12-04 01:56:58 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





h:inputText which is bound to Integer property is submitting value 0 instead of null

(1 个回答)


5年前关闭。




我的要求看起来很简单,但我觉得我在代码中做错了。

当 UI 的 InputText 字段没有传递任何内容时,我只需要在 Oracle 数据库中插入“null”。

PaymantSpecFormPage.xhtml

<h:inputText id="startWeek" value="#{flowScope.paymentSpecVO.paymStartWk}" styleClass="inputTypeForm" maxlength="4"/>

支付规范VO.java
public class PaymentSpecVO extends BaseVO<PaymentSpec> {
private Integer paymStartWk;

public Integer getPaymStartWk() {
return paymStartWk;
}
public void setPaymStartWk(Integer paymStartWk) {
this.paymStartWk = paymStartWk;
}
}

在我的数据库表中
COLUMN_NAME      DATA_TYPE    NULLABLE    DEFAULT_VALUE
------------ --------- -------- -------------
PAYM_START_WK NUMBER(4,0) Yes null

当我在 inputText 中不输入任何内容时,在 Table 中输入的不是 null,而是 0,这在我的业务逻辑中具有不同的含义,请帮助我进行必要的代码更改。

最佳答案

And when I enter nothing in inputText, then instead of null, 0 is getting entered in table



这个问题是可以识别的,因为 Tomcat 的 EL 解析器没有考虑到您正在使用 Integer而不是 int .您需要将以下 VM 参数添加到 Tomcat 启动选项中:

-Dorg.apache.el.parser.COERCE_TO_ZERO=false

也可以看看:
  • h:inputText which is bound to Integer property is submitting value 0 instead of null
  • Work around for faulty INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL in Mojarra JSF 2.1


  • It solved my half of problem, and now its storing null values, but while on retrieve it shows 0 again



    使用 ResultSet#getInt() 可以将此问题识别为隐藏的 JDBC而不是 ResultSet#getObject() .然而,这与第一个问题本质上是一个不同的问题。您没有说明您的数据库访问层是如何设置的,因此很难指出具体的解决方案。

    关于jsf - 整数获取默认值 0,在 Java 中它需要为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18392775/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com