<c:if test =“ $ {d == true}”>无法正常运行

时间:2019-03-09 05:19:44

标签: jsp servlets jstl el

d是servlet文件中的布尔类型。应该返回复选框并显示已选中的复选框,但是它们显示所有行。

该servlet文件中有一个布尔类型a,b,c,d,并应显示为真。 if = test $ {b == true}部分出了问题 Thanks.jsp

  <%@page contentType="text/html" pageEncoding="utf-8"%>
  <!DOCTYPE html>
  <html>
      <c: import url="/includes/header.html"/>

  <body>
      <h1>Thanks for your order</h1>

      <p>Here is the information that you entered:</p>

      <form action="" method="post">

      <label>Name:</label>
      <span>${user.name}</span><br>
      <label>Email:</label>
      <span>${user.email}</span><br>

      <table>
      <tr>
          <th>Cover</th>
          <th>Title</th>
          <th>Price</th>
      </tr>
      <c:if test="${a==true}">

      <tr>
      <td>
          <img src="https://media1.popsugar-assets.com/files/thumbor/vuwUnHVXoO4rzphaNeY0jLp31fM/fit-in/1024x1024/filters:format_auto-!!-:strip_icc-!!-/2016/11/28/937/n/42301552/9c19690c108b07a8_Good_to_Great/i/Good-Great.jpeg" alt="Flowers in Chania" width="75" height="120">
          </td>
      <td>Good to Great</td>
      <td>$19.49</td>

    </tr>
      </c:if>
    <c:if test="${b==true}">
    <tr>
      <td><img src="https://media1.popsugar-assets.com/files/thumbor/1U0aHBbqaiRL9dymuwJSeoFWOMs/fit-in/1024x1024/filters:format_auto-!!-:strip_icc-!!-/2016/11/28/937/n/42301552/d28d1eb93d3532e5_7_Habits/i/7-Habits-Highly-Effective-People.jpeg" alt="" width="75" height="120">
          </td>
      <td>7 Habits Highly Effective People</td>
      <td>$11.99</td>

    </tr>
    </c:if>
    <c:if test="${c==true}">
    <tr>
      <td><img src="https://media1.popsugar-assets.com/files/thumbor/N0nfXcjPKhQpkv_94zstsqdHIZ8/fit-in/1024x1024/filters:format_auto-!!-:strip_icc-!!-/2016/11/28/937/n/42301552/b36de1f065960cff_Pushback/i/Pushback-How-Smart-Women-Ask-Stand-Up-What-Want.jpeg" alt="" width="75" height="120">
          </td>
      <td>Push Back</td>
      <td>$22.99</td>

    </tr>
    </c:if>
    <c:if test="${d==true}">
    <tr>
      <td><img src="https://media1.popsugar-assets.com/files/thumbor/BqKOEy1yzKzCOk_ri1umi9k6jXE/fit-in/1024x1024/filters:format_auto-!!-:strip_icc-!!-/2016/11/28/937/n/42301552/22dfcf9c9bd400c5_Lean_In/i/Lean-Women-Work-Lead.jpeg" alt="" width="75" height="120">
          </td>
      <td>Lean In</td>
      <td>$14.95</td>

    </tr>
    </c:if>
  </table>


      <p>To order another book, click on the Back button in your browser or the Return button shown below.</p>


          <input type="hidden" name="action" value="join">
          <input type="submit" value="Return">
      </form>

  </body>
  </html>

servlet

package murach.email;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import murach.business.User;
import murach.data.UserDB;

public class EmailListServlet extends HttpServlet {

    boolean isContaining(String arr[],String str){

        for(int i = 0; i<arr.length;i++){
            if(str.equals(arr[i])){
                return true;
            }
        }
        return false;
    }

    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        String url = "/index.html";


        // get current action
        String action = request.getParameter("action");
        if (action == null) {
            action = "join";  // default action
        }

        // perform action and set URL to appropriate page
        if (action.equals("join")) {
            url = "/index.jsp";    // the "join" page
        } 
        else if (action.equals("add")) {
            // get parameters from the request
            String name = request.getParameter("name");
            String email = request.getParameter("email");

            // store data in User object
            User user = new User(name, email);


            // validate the parameters
            String message;
            if (name == null || email == null || 
                name.isEmpty() || email.isEmpty() ) {
                message = "Please fill out all text boxes.";
                url = "/index.jsp";
            } 
            else {
                message = "";
                url = "/thanks.jsp";
                UserDB.insert(user);
            }
            request.setAttribute("user", user);
            request.setAttribute("message", message);


            String[] books  = request.getParameterValues("books");

            boolean a = isContaining(books,"book1"); 
            boolean b = isContaining(books,"book2"); 
            boolean c = isContaining(books,"book3"); 
            boolean d = isContaining(books,"book4"); 

            request.setAttribute("a", a);
            request.setAttribute("b", b);
            request.setAttribute("c", c);
            request.setAttribute("d", d);

            System.out.println(a);
            System.out.println(b);
            System.out.println(c);
            System.out.println(d);

            String message2;
            /*if(book1 == 0 && book2 == 0 && book3 == 0 && book4 == 0){
                message2 = "Please order at least one book";
                url = "/index.jsp";
            }
            else {
                message2 = null; 
            } 
            request.setAttribute("message2", message2); */

        }
        getServletContext()
                .getRequestDispatcher(url)
                .forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }    
}

0 个答案:

没有答案