在JSP中使用if-else [英] Using if-else in JSP

查看:1341
本文介绍了在JSP中使用if-else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在浏览器上打印用户名:

I'm using the following code to print the name of the user on the browser:

<body>
  <form>
    <h1>Hello! I'm duke! What's you name?</h1>
    <input type="text" name="user"><br><br>
    <input type="submit" value="submit">&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="reset">
  </form>
  <%String user=request.getParameter("user"); %>
  <%if(user == null || user.length() == 0){
    out.print("I see! You don't have a name.. well.. Hello no name");   
   }
   else {%>
      <%@ include file="response.jsp" %>
   <% } %>  
</body>

response.jsp:

response.jsp:

<body>
    <h1>Hello</h1>
    <%= request.getParameter("user") %>
 body>

每次执行时,消息

我明白了!你没有名字..好吧..你好没有名字

I see! You don't have a name.. well.. Hello no name

即使我没有在文本框。但是,如果我在其中输入任何内容,则会显示response.jsp代码,但我不希望在执行时显示第一条消息。我该如何做到这一点?请修改我的代码。

gets displayed even though I haven't entered anything in the textbox. However if I enter anything in it, then the response.jsp code is displayed, but I don't want the first message to be displayed on execution. How do I make that happen? Please suggest changes in my code.

P.S。我曾在一些问题中读到,不是检查与null的相等性,而是必须检查它是否为等于,以便它不会抛出空指针异常。当我尝试相同的,即 if(user!= null&& ..)时,我得到了 NullPointerException

P.S. I had read in some questions that instead of checking for equality with null, one must check it for not equals, so that it doesn't throw null pointer exception. when I tried the same, i.e. if(user != null && ..), I got NullPointerException.

推荐答案

几乎总是建议不要在JSP中使用scriptlet。他们被认为是糟糕的形式。相反,尝试使用结合EL(表达式语言)的 JSTL (JSP标准标记库)运行你想要做的条件逻辑。作为额外的好处,JSTL还包括其他重要功能,如循环。

It's almost always advisable to not use scriptlets in your JSP. They're considered bad form. Instead, try using JSTL (JSP Standard Tag Library) combined with EL (Expression Language) to run the conditional logic you're trying to do. As an added benefit, JSTL also includes other important features like looping.

而不是:

<%String user=request.getParameter("user"); %>
<%if(user == null || user.length() == 0){
    out.print("I see! You don't have a name.. well.. Hello no name");   
}
else {%>
    <%@ include file="response.jsp" %>
<% } %>

使用:

<c:choose>
    <c:when test="${empty user}">
        I see!  You don't have a name.. well.. Hello no name
    </c:when>
    <c:otherwise>
        <%@ include file="response.jsp" %>
    </c:otherwise>
</c:choose>

此外,除非您计划在代码中的其他位置使用response.jsp,否则可能更容易只需在您的声明中包含html:

Also, unless you plan on using response.jsp somewhere else in your code, it might be easier to just include the html in your otherwise statement:

<c:otherwise>
    <h1>Hello</h1>
    ${user}
</c:otherwise>

另外值得注意。要使用核心标记,必须按如下方式导入:

Also of note. To use the core tag, you must import it as follows:

 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

您希望这样做,以便用户在用户提交用户名时收到消息。最简单的方法是,当user参数为 null 时,根本不打印消息。当用户提交 null 时,您可以进行一些验证以提供错误消息。这是解决问题的更标准方法。要做到这一点:

You want to make it so the user will receive a message when the user submits a username. The easiest way to do this is to not print a message at all when the "user" param is null. You can do some validation to give an error message when the user submits null. This is a more standard approach to your problem. To accomplish this:

在scriptlet中:

In scriptlet:

<% String user = request.getParameter("user");
   if( user != null && user.length() > 0 ) {
       <%@ include file="response.jsp" %>
   }
%>

在jstl:

<c:if test="${not empty user}">
    <%@ include file="response.jsp" %>
</c:if>

这篇关于在JSP中使用if-else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆