2011-06-21 30 views
0

我想实现一个单一的JSP页面的标记是这样一种形式:检测是否JSP负荷首次

JSP:formPay.jsp

<form action="<%=request.getContextPath() + "/Pay.do"%>" method="post" id="guest"> 
    <table> 
     <tr> 
      <td width="150"><span class="required">*</span> First Name:</td> 
      <td><input type="text" name="firstname" value="<%= request.getAttribute("firstname") %>" /> 
      </td> 
     </tr> 
    [...] 
    </table> 
<input class="button" type="submit" value="Confirm" name="btnConfirm"> 
</from> 

的Servlet:PayCommand。 java的

public class PayCommand implements Command { 

    @Override 
    public HttpServletRequest execute(HttpServletRequest request) 
      throws ServletException, IOException { 

     boolean errorWithField = false; 

     String paramFirstName = ""; 
     String paramLastName = ""; 
     String paramEmail = ""; 
     String paramCity = ""; 
     String paramAddress = ""; 

     try { 
      paramFirstName = request.getParameter("firstname"); 
      paramLastName = request.getParameter("lastname"); 
      paramEmail = request.getParameter("email"); 
      paramCity = request.getParameter("city"); 
      paramAddress = request.getParameter("address"); 
     } catch (Exception e) { 
      request.setAttribute("jsp", "formPay"); 
      return request; 
     } 

     if (paramFirstName==null || paramFirstName.equals("")) { 
      errorWithField = true; 
     } 
     if (paramLastName==null || paramLastName.equals("")) { 
      errorWithField = true; 
     } 
     if (paramEmail==null || paramEmail.equals("")) { 
      errorWithField = true; 
     } 
     if (paramCity==null || paramCity.equals("")) { 
      errorWithField = true; 
     } 
     if (paramAddress==null || paramAddress.equals("")) { 
      errorWithField = true; 
     } 

     // if errorWithField==true, reload the formPay.jsp 
     if (errorWithField) { 
      request.setAttribute("message", "You have to fill out all of the fields."); 

      request.setAttribute("jsp", "formPay"); 
      request.setAttribute("firstname", paramFirstName); 
      request.setAttribute("lastname", paramLastName); 
      request.setAttribute("email", paramEmail); 
      request.setAttribute("city", paramCity); 
      request.setAttribute("address", paramAddress); 

     } else {   
      // if not, go to the confirm page, everything is ok. 
      request.setAttribute("jsp", "confirmation"); 
      request.setAttribute("firstname", paramFirstName); 
      request.setAttribute("lastname", paramLastName); 
      request.setAttribute("email", paramEmail); 
      request.setAttribute("city", paramCity); 
      request.setAttribute("address", paramAddress); 
     } 
     return request; 
    } 
} 

的问题是,当JSP负荷首次,我想它被解释为如果没有错误,但errorWithField原来是true,那么即使在用户填写表单之前,错误消息也会显示。

第二个问题是,JSP将为已填写的字段取值,但如果没有任何内容(包括第一次加载JSP)将返回null。我该如何处理这个问题?

EDIT 请注意,Pay.do表单动作被重定向到PayCommand.java,其被处理的前端控制器模式。 (未显示)

回答

2
  1. 第一个问题,我认为你可以使用另一个.do操作只转发到jsp页面,而不需要将errorWithField属性设置为true。
  2. 第二个问题,你可以试试这个:

    <%= request.getAttribute( “名字”)== NULL? “”:request.getAttribute(“firstname”)%>

+0

谢谢你对问题2的回答,对于问题1,我最终在JSP文件中创建了一个隐藏字段。 –

3

您可以向窗体添加隐藏字段,并检查它是否有值。如果确实如此,表单将被重新发布;否则,它是第一次加载(你应该跳过错误检查的东西)。