加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-15 00:05:21 所属栏目:Java 来源:网络整理
导读:参见英文答案 h:inputText which is bound to Integer property is submitting value 0 instead of null1 我的要求看起来很简单,但我觉得我在代码中做错了. 所有我需要的是在没有从UI的InputText字段传递的情况下在Oracle数据库中插入“null”. PaymantSpecF
参见英文答案 > h:inputText which is bound to Integer property is submitting value 0 instead of null1
我的要求看起来很简单,但我觉得我在代码中做错了.

所有我需要的是在没有从UI的InputText字段传递的情况下在Oracle数据库中插入“null”.

PaymantSpecFormPage.xhtml

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

PaymentSpecVO.java

public class PaymentSpecVO extends BaseVO<PaymentSpec> {
   private Integer paymStartWk;

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

在我的DB表中

COLUMN_NAME      DATA_TYPE    NULLABLE    DEFAULT_VALUE
------------     ---------    --------    -------------
PAYM_START_WK    NUMBER(4,0)    Yes             null

当我在inputText中输入任何内容时,而不是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

这个问题可以被JDBC识别,使用ResultSet#getInt()而不是ResultSet#getObject().然而,这与第一个问题基本上是一个不同的问题.你没有告诉任何关于你的数据库访问层的设置,所以很难指出一个具体的解决方案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读