java - 检查 Textarea 字段是否为空。如果是这样,请不要发表评论

标签 java html jsp

我的代码需要一些帮助。 我有一个文本区域,它只应在选择单选按钮时显示。然后在文本区域中输入的任何内容都应发送并存储在数据库中。 我的两个单选按钮是:

enter image description here

如果选择了No,那么会出现文本区域:

enter image description here

其代码如下所示:

<h1>Accept or Decline Invitation</h1>
      <%=txt.displayCustomContent( "accept_text","acceptsection" )%>

      <fieldset >
      <label>Will you be attending the <%=formFields.getValue("programName")%> in <%=formFields.getValue("destination")%> <%=formFields.getValue("programDates")%>?</label>
      </fieldset>
      <fieldset>
     <input id="yes" type="radio" name="attending" class="show-hide" value="Yes" <%= formFields.getRadioValue("attending","Yes")%>/> Yes, I will attend. I have read the instructions and I will register now.  
      </fielset>
      <fieldset>
      <input id="no" type="radio" name="attending" class="show-hide" value="No" <%= formFields.getRadioValue("attending","No")%>/> No, I will not attend.
      </fieldset>
      <br/>
      <div id="show-me" style="display:none;"> 
      <fieldset>
      <label>If you will not be attending, please provide a brief explanation below (maximum 40 characters).
          <textarea name="declineReason" id="declineReason" cols="25" rows="3" onKeyDown="textCounter(this.form.declineReason,40);" onKeyUp="textCounter(this.form.declineReason,40);"><%= formFields.getTextAreaValue("declineReason")%></textarea>
      </label>
      </fieldset>
      </div>

      <%=txt.displayCustomContent( "accept_textbottom","acceptsectionbottom" )%>


      <div id="registration-navigation">
        <input name="" type="submit" value="next &#62;&#62;" class="button"/>
      </div>

这就是我的问题所在.. 即使我选择 Yes,文本区域也会发回信息并将其存储在数据库中。

enter image description here

存储它的代码如下所示:

 if(formFields.exists("declineReason"))
    {
        awsl.setLoginComment("Decline reason is: " + formFields.getValue("declineReason"));
    }

所以我的问题是如何更改它以便仅在文本区域中包含文本时才将信息发送到数据库? 我试图检查并查看 if 语句中的文本区域是否为 NULL,但这似乎不起作用。

最佳答案

根据@Stultuske 所说的内容,我建议仍然检查空状态以避免出现空指针异常的机会。但正如他们所说,一旦它被实例化,它就永远不会返回 null,因此您需要检查它是否为空,这实际上只是检查字段中文本的长度。

if(jTextArea.getText() == null || jTextArea.getText().isEmpty())
{
    // your code here
}

关于java - 检查 Textarea 字段是否为空。如果是这样,请不要发表评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30031437/

相关文章:

html - 在响应式模板上控制文本宽度的限制

javascript - Stripe 元素快速入门示例未正确呈现

javascript - 需要禁用 JSP 中复选框名称与隐藏字段相同的复选框

java - 如何在java中生成从0000到9999的4位随机数

java - Android 缺少 android.net.StaticIpConfiguration。无法设置新实例或导入

javascript - 对齐文本中的图像而行高没有问题(聊天框中的笑脸)

javascript - 如何在jsp中将值传递给bootbox警报功能

java - XSS 漏洞 <c :out> tags in JSP pages

java - JVM 和 Java Linux 进程

java - 使用 java(Threads) 从方法返回值