【问题标题】:Why are all forms displayed even if the <c:if> tags evaluate to false?为什么即使 <c:if> 标签评估为假,所有表单也会显示?
【发布时间】:2021-10-05 09:50:52
【问题描述】:

我想使用单个 jsp 文件来注册客户、供应商和托运人,但是当我使用 标签运行程序时,尽管 System.out.print(type) 显示了值,但所有表单都会显示“供应商”。

我能做些什么来解决这个问题?

下面我展示了 Supplier.jsp 和 Sign_in.jsp 的代码。对于客户和托运人的注册,jsp的实现方式与Supplier.jsp相同,只修改字符串“customer”和“shipper”的类型变量的值。这些页面将为每个 Customer、Shipper 和 Supplier 类显示更具体的值。

Supplier.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
    <title>Bienvenido!</title>
</head>
<body>
       <h2>Sign in</h2>
    <a href="Sign_in.jsp">No account yet? Sign in</a>
    
    <% request.getSession().setAttribute("type", "supplier"); %> 
    
</body>
</html>

Register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Sign in</title>
</head>
<body>
    <h1>Sign in</h1>

    <% String type = (String) request.getSession().getAttribute("type");
    String supplier = "supplier";
    String shipper = "shipper";
    String client = "client";
    %>
    
    <% System.out.println(type.equals(supplier)); %>
    
        <c:if test="${type == supplier}">
        <h2>I'm a supplier</h2>
        <form action="RegisterServlet" method="post">
            <input type="email" name="email" placeholder="Email"> 
            <input type="password" name="password" placeholder="Password">
            <button type="submit">Register</button>
        </form>
    </c:if>

    <c:if test="${type == shipper}">
        <h2>I'm a shipper</h2>
        <form action="RegisterServlet" method="post">
            <input type="email" name="email" placeholder="Email"> 
            <input type="text" name="user" placeholder="User">
            <input type="password" name="password" placeholder="Password">
            <button type="submit">Register</button>
        </form>
    </c:if>

    <c:if test="${type == client}">
        <h2>I'm a client</h2>
        <form action="RegisterServlet" method="post">
            <input type="email" name="email" placeholder="Email"> 
            <input type="text" name="user" placeholder="User">
            <input type="password" name="password" placeholder="Password">
            <input type="text" name="city" placeholder="City"> 
            <input type="text" name="address" placeholder="Address"> 
            <input type="text" name="country" placeholder="Country"> 
            <button type="submit">Register</button>
        </form>
    </c:if>
</body>
</html>

【问题讨论】:

    标签: java jsp jsp-tags


    【解决方案1】:

    您需要更改 test 条件以使用常量作为

    <c:if test="${type == 'supplier'}">
    <c:if test="${type == 'shipper'}">
    <c:if test="${type == 'client'}">
    

    因为suppliershipperclient局部变量,而不是作用域属性

    如果在此更改后没有呈现任何表单,则您的 type 属性设置不正确。检查您的hrefs 以查看您是否正在导航到正确的 JSP 页面。您正在调试 Register.jsp,但您的链接指向 Sign_in.jsp,因此这也可能是另一个问题。


    @MauricePerry 提出了一个很好的观点,即您应该避免使用 ,因为您已经在使用 JSTL 标记。例如,您的代码添加了一个 session 属性

    <% request.getSession().setAttribute("type", "supplier"); %>
    

    可以用&lt;c:set&gt;JSTL 标记替换。

    <c:set var="type" value="supplier" scope="session" />
    

    的使用在 view 层是非常不受欢迎的,因此,请尽量将它们的使用限制为仅用于快速调试。

    【讨论】:

    • 是的。我要补充一点,如果您使用的是 JSTL 标记,则不需要脚本。避免使用脚本。
    • 感谢@MauricePerry 的评论。我现在用一个例子更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 2018-07-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-04
    • 2012-04-22
    相关资源
    最近更新 更多