【问题标题】:Error handling of a Custom Form Handler in ATGATG 中自定义表单处理程序的错误处理
【发布时间】:2014-12-22 13:50:36
【问题描述】:

我是 ATG 的新手。我正在尝试使用我自己的 RepositoryFormHandler 。但我无法对表单进行验证。

这是我的 .java 文件:

public class MyLoginBean extends RepositoryFormHandler {

    private String logname;
    private String logpwd;
    private String message;

    public String getLogname() {
        return logname;
    }

    public void setLogname(String logname) {
        this.logname = logname;
    }

    public String getLogpwd() {
        return logpwd;
    }

    public void setLogpwd(String logpwd) {
        this.logpwd = logpwd;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean handleLogname(DynamoHttpServletRequest pRequest,
            DynamoHttpServletResponse pResponse) throws ServletException,
            IOException {
        boolean tf=true;
        if(logname.isEmpty() || logname==null)
        {
            tf=false;
            setMessage("User name can't empty");
        }

        System.out.println("inside logname");
        return tf;
    }

    public void handleFormException(DropletFormException exception,
            DynamoHttpServletRequest request, DynamoHttpServletResponse response) {
        // TODO Auto-generated method stub
        super.handleFormException(exception, request, response);
    }

}

这是我的 .jsp 文件:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/dspTaglib" prefix="dsp" %>
<dsp:importbean bean="/atg/dynamo/droplet/ErrorMessageForEach"/>
<dsp:importbean bean="/dynamusic/MyLoginBean"/>
<!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=ISO-8859-1">
<title>Custom Login</title>
</head>
<body>
<dsp:form style="color:white">
    <table style="background:#3b5998">
        <tr>
            <td>
                <ul>
                    <dsp:droplet name="ErrorMessageForEach">
                        <dsp:param bean="MyLoginBean.formExceptions" name="exceptions"/>
                        <dsp:oparam name="output">
                            <li>
                                <dsp:valueof param="message"/>
                            </li>
                        </dsp:oparam>
                    </dsp:droplet>
                </ul>
            </td>
        </tr>
        <tr>
            <td>
                User Name:
            </td>
            <td>
                Password:
            </td>
        </tr>
        <tr>
            <td>
                <dsp:input type="text" name="logname" bean="MyLoginBean.logname"> </dsp:input>
            </td>
            <td>
                <dsp:input type="password" name="logpwd" bean="MyLoginBean.logpwd"> </dsp:input>
            </td>
            <td>
                <dsp:input type="submit" bean="MyLoginBean.login"> </dsp:input>
            </td>
        </tr>
    </table>
</dsp:form>
</body>
</html>

到目前为止,我已经尝试了所有这些,但仍在尝试其他方法。 请提出解决方案,并告诉我粘贴在此处的代码中的错误(如果有)。

【问题讨论】:

  • 您对logname 的访问器将与您的handleLogname 方法发生冲突,因为这两个方法都将通过MyLoginBean.logname 的形式引用。看起来您随后通过在 JSP 上使用 MyLoginBean.login 但在 Java 代码中没有相关的 handle 方法来解决此问题。另请参阅 Patrick 的 cmets 以获得更多指导。

标签: java atg


【解决方案1】:
  1. 不要覆盖handleFormException
  2. 不使用 setMessage,而是使用 ATG 的内置行为。所有表单处理程序都从 GenericFormHandler 超类继承表单异常向量。要添加错误,请使用:

addFormException(new DropletException("Your error message"));

然后,在你的方法结束时,调用:

return checkFormRedirect(getSuccessUrl(), getFailUrl(), pRequest, pResponse);

这会检查是否添加了任何表单异常,如果是,则重定向到failUrl,否则重定向到successUrl。

  1. 按照惯例,您应该将表单处理程序命名为 *FormHandler,例如 ProfileFormHandler、BillingInfoFormHandler、PaymentInfoFormHandler 等。

希望这会有所帮助。见http://docs.oracle.com/cd/E22630_01/Platform.1002/apidoc/atg/droplet/GenericFormHandler.html#getFormExceptions()

【讨论】:

    猜你喜欢
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 2011-06-01
    • 1970-01-01
    • 2011-04-21
    • 2013-01-04
    • 1970-01-01
    相关资源
    最近更新 更多