如何验证操作方法中的空字段?

发布于 2024-12-16 11:50:38 字数 671 浏览 4 评论 0原文

我有一个输入字段和一个按钮。我想在执行按钮操作之前检查 textinput 是否有效。如果有效,我将呈现一条响应消息。我有这样的代码:

public void submitReportRequest() {
    if(nameField!=null){
        System.out.println("aaaaaaaaaaaaa");
        submitted=true;
    }
    if(nameField == null){
        System.out.println("report name is null!!!!!!");
    }
}

但从控制台我刚刚得到:

[#|2011-11-18T15:22:49.931+0200|INFO|glassfishv3.0|null|_ThreadID=21;_ThreadName=Thread-1;|aaaaaaaaaaaaa|#]

nameField 为空时,我在控制台中接收不到任何内容,只是使用 nameField< 的验证消息重新渲染页面/代码>。我从 JSF 生命周期知道,如果验证阶段失败,那么它会直接跳转到渲染响应阶段,并且永远不会达到按钮操作。但在这种情况下我怎样才能实现我的目标呢?

I have an input field and a button. I want to check if the textinput is valid before executing the button action. If it is valid I will render a response message. I have a code like this:

public void submitReportRequest() {
    if(nameField!=null){
        System.out.println("aaaaaaaaaaaaa");
        submitted=true;
    }
    if(nameField == null){
        System.out.println("report name is null!!!!!!");
    }
}

but from the console I just get:

[#|2011-11-18T15:22:49.931+0200|INFO|glassfishv3.0|null|_ThreadID=21;_ThreadName=Thread-1;|aaaaaaaaaaaaa|#]

when the nameField is empty, I receive nothing in the console just page is re-rendered with the validation message of nameField. I know from the JSF life cycle if the validation phase fails then it jumps directly to the render response phase and button action is never reached. But how can I achieve my objective in this case?

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

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

发布评论

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

评论(1

错爱 2024-12-23 11:50:38

提交的空值默认为空字符串,而不是 null。相反,您需要通过 String#isEmpty()

if (nameField.isEmpty()) {
    // Name field is empty.
} else {
    // Name field is not empty.
}

您可能还想覆盖空格。在这种情况下,请添加 trim()

if (nameField.trim().isEmpty()) {
    // Name field is empty or contained spaces only.
} else {
    // Name field is not empty and did not contain spaces only.
}

请注意,String#isEmpty() 是在 Java 1.6 中引入的。如果由于某种原因您仍然使用 Java 1.5 或更早版本,那么您需要检查 String#length() 代替。

if (nameField.length() == 0) {
    // Name field is empty.
} else {
    // Name field is not empty.
}

但是,这不是所需字段验证的正常方式。您应该将 required="true" 属性放在输入字段上。

<h:inputText id="name" value="#{bean.name}" required="true" />
<h:message for="name" />

这样,JSF 将自行验证它并显示适当的消息,并跳过操作方法调用。

另请参阅:

Empty submitted values default to empty strings, not null. Instead, you need to check if the string is empty by String#isEmpty():

if (nameField.isEmpty()) {
    // Name field is empty.
} else {
    // Name field is not empty.
}

You perhaps want to cover blank spaces as well. In that case, add trim():

if (nameField.trim().isEmpty()) {
    // Name field is empty or contained spaces only.
} else {
    // Name field is not empty and did not contain spaces only.
}

Note that the String#isEmpty() is introduced in Java 1.6. If you're still on Java 1.5 or older for some reason, then you need to check String#length() instead.

if (nameField.length() == 0) {
    // Name field is empty.
} else {
    // Name field is not empty.
}

However, that's not the normal way of required field validation. You should put the required="true" attribute on the input field instead.

<h:inputText id="name" value="#{bean.name}" required="true" />
<h:message for="name" />

This way JSF will validate it by itself and display the appropriate message and will skip the action method invocation.

See also:

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