【问题标题】:Detecting if JSP loads for the first time检测 JSP 是否第一次加载
【发布时间】:2011-06-21 02:26:06
【问题描述】:

我想实现一个像这样的表单的 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。我该如何处理这个问题?

编辑 请注意Pay.do 表单操作被重定向到PayCommand.java,这是处理前端控制器模式。 (未显示)

【问题讨论】:

    标签: forms jsp servlets setattribute


    【解决方案1】:

    您可以在表单中添加一个隐藏字段,并检查它是否有值。如果是,则表格将被发回;否则,它是第一次加载(你应该跳过错误检查的东西)。

    【讨论】:

      【解决方案2】:
      1. 第一个问题,我认为你可以使用另一个.do动作只转发到jsp页面而不设置属性errorWithField为true。
      2. 第二个问题,你可以试试这个:

      【讨论】:

      • 感谢您对问题2的回答,对于问题1,我最终在JSP文件中创建了一个隐藏字段。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-12
      • 2019-02-15
      • 2018-07-25
      • 1970-01-01
      相关资源
      最近更新 更多