如果用户不发送胸腺的输入,我该如何设置默认值

发布于 2025-01-28 15:00:28 字数 754 浏览 1 评论 0原文

现在 - 我的html上有一个输入字段,即当页面加载时,胸腺上胸腺上呈“ 0”。如果由于某种原因删除了“ 0”并提交表单 - 我会得到一个numberFormateXception。

如果表格是在没有输入表格中任何内容的情况下提交的,我是否可以自动使其“ 0”?

这是我的html表单(我的th:对象是$ {npf}),

<div class="col-md-2 form-outline">
     <input class="form-control" type="text" id="addConcealed" th:field="${npf.numConcealed}" placeholder="C" />
     <label class="form-label text-muted" for="addConcealed">Concealed</label>
</div>

这是我遇到的错误:

[Failed to convert property value of type 'java.lang.String' to required type 'long' for property 'numConcealed'; nested exception is java.lang.NumberFormatException: For input string: ""]

无论如何,如果输入字段中没有任何内容,而不是误解并让用户输入数字,则我可以将其默认为0?

Right now - I have a input field on my html that Thymeleaf sticks a '0' in when the page loads. If for some reason that '0' is erased and the form is submitted - I get a NumberFormatException.

Is there a way I can automatically make it '0' if the form is submitted without anything in the input form?

Here's my html form (my th:object is ${npf})

<div class="col-md-2 form-outline">
     <input class="form-control" type="text" id="addConcealed" th:field="${npf.numConcealed}" placeholder="C" />
     <label class="form-label text-muted" for="addConcealed">Concealed</label>
</div>

Here's the error I get:

[Failed to convert property value of type 'java.lang.String' to required type 'long' for property 'numConcealed'; nested exception is java.lang.NumberFormatException: For input string: ""]

Anyway I can have it default to 0 if there's nothing in the input field rather than erroring out and making the user enter a number?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2025-02-04 15:00:28

有几种方法可以处理以下方法:

  1. 将您的变量numcealed转换为long而不是long。擦除字段时处理零值。

  2. 使用JavaScript,捕获提交,只需将字段值更改为0,如果他们提交空白文本字段。

  3. 创建自己的字符串 to int 转换器并将其注册为该字段。应该以与普通转换器相同的方式(无论是什么),除非它将返回为nulls。

There are a couple ways to handle this:

  1. Turn your variable numConcealed into an Long instead of a long. Deal with a null value when they erase the field.

  2. Use JavaScript, capture the submit just change the field value back to 0 if they submit a blank text field.

  3. Create your own String to int Converter and register it for that field. Should work the same way as the normal converter (whatever that is) except it would return 0 for nulls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文