C: 何时不适用于 jsp 中的列表值

C: when doesn't work with list values in jsp

下面是代码,In quentionAnsers[0] 在 have's single 作为字符串值,当我显示 ${quentionAnsers[0]} 它在同一页中打印 single 但是它不适用于 c:when,在我的 Jsp 页面中,我得到了复选框和单选按钮,而我只希望有复选框。请帮助我!

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
    <html> 
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Login Form</title>
<link rel="stylesheet" href="application.css" type="text/css">
</head>
<body>
<div align="center">
    <div class="header"></div>
    <div class="mainbody">

        <H4>Please answer the following quetions</H4>
        <form method="post" action="demoServlet">
            <table cellspacing="5" cellpadding="5">
                <tr>
                    <td>${quentionAnsers[1]}</td>
                </tr>
                <tr>
                    <td><c:set var="questionAnswers" value="${quentionAnsers[0]}" />
                        <c:choose>
                            <c:when test="${questionAnswers == 'single'}">
                                <input type="checkbox" name="language" value="English" />English
                                <input type="checkbox" name="language" value="French" />French
                            </c:when>
                            <c:otherwise>
                                <input type="radio" name="gender" value="Male" />Male
                                <input type="radio" name="gender" value="Female" />Female</td>
                        </c:otherwise>
                    </c:choose>
                    </td>

                </tr>

            </table>
            <br />
            <br />
  <input type="submit" value="Submit" />
        </form>
         </div>
       </div>
       </body>
        </html>

如果它打印单行且没有额外的空格

你的测试应该像你的代码一样 == 或 eq

<c:when test="${quentionAnsers[0] == 'single'}">

如果您同时拥有复选框和单选按钮,那就很奇怪了!

您必须在页面顶部导入 jstl 标签库:

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

这是新代码:

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <html> 
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>Login Form</title>
<link rel="stylesheet" href="application.css" type="text/css">
</head>
<body>
<div align="center">
    <div class="header"></div>
    <div class="mainbody">

        <H4>Please answer the following quetions</H4>
        <form method="post" action="demoServlet">
            <table cellspacing="5" cellpadding="5">
                <tr>
                    <td>${quentionAnsers[1]}</td>
                </tr>
                <tr>
                        <c:choose>
                            <c:when test="${quentionAnsers[0] == 'single'}">
                                <input type="checkbox" name="language" value="English" />English
                                <input type="checkbox" name="language" value="French" />French
                            </c:when>
                            <c:otherwise>
                                <input type="radio" name="gender" value="Male" />Male
                                <input type="radio" name="gender" value="Female" />Female</td>
                        </c:otherwise>
                    </c:choose>
                    </td>

                </tr>

            </table>
            <br />
            <br />
  <input type="submit" value="Submit" />
        </form>
         </div>
       </div>
       </body>
        </html>