整数获取默认值0,其中Java中需要为NULL [英] Integer getting default value 0 where it required to be NULL in Java

查看:754
本文介绍了整数获取默认值0,其中Java中需要为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My requirement looks simple, but I feel I'm doing something wrong in code.

所有我需要的是在没有任何东西的情况下在Oracle数据库中插入null从UI的InputText字段传递。

All I need is to insert "null" in Oracle Database when nothing is passed from InputText field of UI.

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;
   }
}

在我的数据库表中

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

当我在inputText中没有输入任何内容时,那么在表格中输入0而不是null,这在我的业务逻辑中有不同的含义,请帮我做必要的代码更改。

And when I enter nothing in inputText, then instead of null, 0 is getting entered in Table, which has different meaning in my business logic, please help me to do necessary code changes.

推荐答案


当我在inputText中没有输入任何内容时,则输入0而不是null在表中

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

This problem is recognizable as Tomcat's EL parser not taking into account that you're using Integer instead of int. You need to add the following VM argument to Tomcat startup options:

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



参见:




  • h:绑定到Integer属性的inputText提交值0而不是null

  • 解决Mojarra JSF 2.1中错误的INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL

  • See also:

    • 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

    • 它解决了我的一半问题,现在它存储空值,但在检索时它再次显示0

      使用回复sultSet #getInt()而不是 ResultSet#getObject()。然而,这与第一个问题基本上是不同的问题。您没有说明如何设置数据库访问层,因此很难指出具体的解决方案。

      This problem is recognizable as JDBC under the covers using ResultSet#getInt() instead of ResultSet#getObject(). This is however essentially a different problem than the first one. You didn't tell anything about how your database access layer is setup, so it's hard to point out a concrete solution.

      这篇关于整数获取默认值0,其中Java中需要为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆