2014-10-27 53 views
1

我是ATG的新手。我正在嘗試使用我自己的RepositoryFormHandler。但我無法在表單上進行驗證。ATG中的自定義表單處理程序的錯誤處理

這是我的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> 

這一切到目前爲止,我已經試過,仍然嘗試別的東西。 請建議解決方案,並告訴我在這裏粘貼的代碼中的錯誤,如果有的話。

+0

您對'logname'的訪問者將與您的'handleLogname'方法衝突,因爲這兩者都將被表單稱爲「MyLoginBean.logname」。看起來您隨後通過在JSP上使用'MyLoginBean.login'來解決此問題,但在Java代碼中沒有相關的'handle'方法。另請參閱Patrick的評論以獲取更多指導。 – radimpe 2014-10-28 08:31:14

回答

7
  1. 不要覆蓋handleFormException
  2. 而不是使用setMessage的,使用ATG的內置行爲。所有表單處理程序都從GenericFormHandler超類繼承了一個Vector異常表單。要添加一個錯誤,使用:

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

然後,在你的方法結束時,撥打:如果任何形式的例外已添加

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

此檢查,如果所以,重定向到failUrl,否則重定向到successUrl。

  • 按照慣例,你應該爲您的表單處理程序* FormHandler,例如ProfileFormHandler,BillingInfoFormHandler,PaymentInfoFormHandler等
  • 希望這有助於。請參閱http://docs.oracle.com/cd/E22630_01/Platform.1002/apidoc/atg/droplet/GenericFormHandler.html#getFormExceptions()

    相關問題