如果用户不发送胸腺的输入,我该如何设置默认值
现在 - 我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几种方法可以处理以下方法:
将您的变量
numcealed
转换为long
而不是long
。擦除字段时处理零值。使用JavaScript,捕获提交,只需将字段值更改为0,如果他们提交空白文本字段。
创建自己的
字符串
toint
转换器并将其注册为该字段。应该以与普通转换器相同的方式(无论是什么),除非它将返回为nulls。There are a couple ways to handle this:
Turn your variable
numConcealed
into anLong
instead of along
. Deal with a null value when they erase the field.Use JavaScript, capture the submit just change the field value back to 0 if they submit a blank text field.
Create your own
String
toint
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.