【问题标题】:How can i remove the null from textarea when request.GetAttribute is null, and how can i modify the visibility of the label from the servlet当 request.GetAttribute 为 null 时,如何从 textarea 中删除 null,以及如何从 servlet 修改标签的可见性
【发布时间】:2015-04-29 19:41:46
【问题描述】:

当 request.GetAttribute 为 null 时,如何从 textarea 中删除 null,以及如何从 servlet 修改标签的可见性? 在输入 textarea 上它不显示 request.getattribute 值,在输出 textarea 上它显示值但是当 request.getattirbute 为 null 时,在 textarea 上打印 null 并且我想删除它,如何?

    <label>Type word/s here:</label></br>
    <textarea name="Input" id="styleid">
    <%
    String msg=(String)request.getAttribute("Input");
    if(msg==null)
    {
        msg="";
    }
    %>
    </textarea> </br>
    <input type="submit" class="styled-button-2" value="Translate" name="query" /> </br>
    <textarea name="Output" id="styleid" text="" disabled>
    <%=
    request.getAttribute("Output")
    %>
    </textarea> 

关于标签的可见性 JSP

     <label for="Syllabication" class="
     <%=request.getAttribute("Visibility")%>">Syllabication</label>

小服务程序

    response.setContentType("text/html");
    request.setAttribute("Visbility","hidden");
    request.getRequestDispatcher("eng-chav.jsp").forward(request, response);

还有隐藏类

    <style>
    .hidden{
       visibility:hidden;
    }
    </style>

【问题讨论】:

  • 考虑使用核心jstl c:if
  • 我不确定你能不能试试这个 request.getAttribute("Output")!=null。在这种情况下,如果不是 null ,它将显示文本区域和值。如果为null,它将显示文本区域并且值为空
  • 返回布尔结果并显示

标签: java javascript css jsp servlets


【解决方案1】:
<%
String msg=(String)request.getAttribute("Input");
if(msg==null)
{
    msg="";
}
%>

这不会输出任何内容,因为该值仅存储在msg 中,并且不会在任何地方打印出来。您可能应该添加:

<%=
    msg
%>

在另一个文本区域中,您有: 当您没有称为“输出”的属性时,它将打印出null。所以这可能是您看到的null 的来源。

至于可见性问题,当您想让元素再次可见时,您只需添加另一个类即可。例如:

<style>
.hidden {
   visibility: hidden;
}
.visible {
   visibility: visible;
}
</style>

然后,通过编写以下代码使元素在 servlet 代码中可见:

request.setAttribute("Visbility","hidden");

【讨论】:

  • 当“输出”为空时,如何删除文本区域中的空文本?
  • 与“输入”相同。使用&lt;% String msg=(String)request.getAttribute("Output"); if (msg == null ) msg = ''; %&gt; 然后&lt;%= msg %&gt;
  • 明白了,谢谢,当请求被打印到 textarea 时,第一行总是有一个空行
猜你喜欢
  • 1970-01-01
  • 2021-01-04
  • 1970-01-01
  • 1970-01-01
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
  • 2015-09-11
  • 2018-01-29
相关资源
最近更新 更多