当我单击“提交”按钮时,无法在jsp页面上显示错误消息

时间:2019-03-24 19:35:12

标签: jsp servlets jstl

当输入字段为空或为空时,我想在jsp文件上显示错误消息。我在Servlet中收到错误消息,但无法在jsp文件上显示该消息。当我单击“保存”或“更新”以供学生使用,并且其信息为空时,无法看到“提交”按钮。

我该如何解决?

这是我的servlet和jsp页面。

private void addStudent(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // read student info from form data
        String firstName = request.getParameter("studentNameInput");
        String lastName = request.getParameter("studentLastNameInput");
        String email = request.getParameter("studentEmailInput");       


        // Image
        InputStream inputStream = null; // input stream of the upload file
        Part filePart = request.getPart("imageFileUpload");

        if(firstName!= "" && lastName!="" && email!="" && filePart.getSize()!=0) {

            if (filePart != null && filePart.getSize() > 0) {
                // prints out some information for debugging
                System.out.println(filePart.getName());
                System.out.println(filePart.getSize());
                System.out.println(filePart.getContentType());

                // obtains input stream of the upload file
                inputStream = filePart.getInputStream();
            }

            // Convert Byte from InputStream    
            //byte[] image = IOUtils.toByteArray(inputStream);

            // create a new student object
            Student student = new Student(firstName, lastName, email);

            // add the student to the database
            studentUtil.addStudent(student,inputStream);

            // send back to main page (the student list)
            // send back to main page (the student list)
            // SEND AS REDIRECT to avoid multiple-browser reload issue
            response.sendRedirect(request.getContextPath() + "/StudentServlet?command=LIST");
        }else {

            checkInputField(request, response, firstName, lastName, email, filePart);
        }

    }

checkInputField

private void checkInputField(HttpServletRequest request, HttpServletResponse response, String firstName,
        String lastName, String email, Part filePart) throws IOException, ServletException {
    // First Name
    if(firstName.equals("")) {
        // place student in the request attribute
        String errorMessage = "Please enter first name";
        request.setAttribute("errorMessageFirstName", errorMessage);
    }

    // Last Name
    if(lastName.equals("")) {
        String errorMessage = "Please enter last name";
        request.setAttribute("errorMessageLastName", errorMessage);
    }

    // Email
    if(email.equals("")) {
        String errorMessage = "Please enter email address";
        request.setAttribute("errorMessageEmail", errorMessage);
    }

    // Image 
    if(filePart.getSize()==0) {
        String errorMessage = "Please upload image file";
        request.setAttribute("errorMessageFilePart", errorMessage);
    }

    // send back to main page (the student list)
    // SEND AS REDIRECT to avoid multiple-browser reload issue
    response.sendRedirect(request.getContextPath() + "/StudentServlet?command=LIST");

}

jsp文件

<form action="StudentServlet" method="POST" enctype="multipart/form-data" name="studentForm" id="studentForm">

      <!-- ADD FORM -->
      <c:if test='${param.command != "LOAD"}'>
        <input type="hidden" name="command" value="ADD" />
      </c:if>

      <!-- UPDATE FORM -->
      <c:if test='${param.command == "LOAD"}'>
        <input type="hidden" name="command" value="UPDATE" />
        <input type="hidden" name="ID" value="${param.ID}" />
      </c:if>

      <div class="form-group row">
        <label for="studentName" class="col-sm-2 col-form-label">Student Name:</label>
        <div class="col-sm-10">

          <c:if test='${param.command == "LOAD"}'>
            <input type="text" class="form-control" id="studentNameInput" name="studentNameInput" value="${student.firstName}">
          </c:if>

          <c:if test='${param.command != "LOAD"}'>
            <input type="text" class="form-control" id="studentNameInput" name="studentNameInput" placeholder="Enter Student Name" required>
          </c:if>

          <c:if test="${not empty errorMessageFirstName}">
              <div class="alert alert-danger col-sm-3">
                ${errorMessageFirstName}     
              </div>
          </c:if>

        </div>
      </div>
<!--  <button type="submit" class="btn btn-primary">Submit</button>-->
      <c:if test='${param.command == "LOAD"}'>
          <tr>
              <button type="submit" class="btn btn-primary">Update</button>
          </tr>
      </c:if>
      <c:if test='${param.command != "LOAD"}'>
          <tr>
              <button type="submit" class="btn btn-primary">Save</button>
          </tr>
      </c:if>
    </form>

0 个答案:

没有答案