struts2 Validator 整合freemarker 取得FieldError的方法

阅读更多

struts2 Validator 整合freemarker 取得FieldError的方法

 

对源代码进行跟踪:

protected void addActionError(Object object)
    {
        validatorContext.addActionError(getMessage(object));
    }

    protected void addFieldError(String propertyName, Object object)
    {
        validatorContext.addFieldError(propertyName, getMessage(object));
    }



public interface ValidatorContext
    extends ValidationAware, TextProvider, LocaleProvider
{

    public abstract String getFullFieldName(String s);
}

  

 

public interface ValidationAware
{

    public abstract void setActionErrors(Collection collection);

    public abstract Collection getActionErrors();

    public abstract void setActionMessages(Collection collection);

    public abstract Collection getActionMessages();

    public abstract void setFieldErrors(Map map);

    public abstract Map getFieldErrors();

    public abstract void addActionError(String s);

    public abstract void addActionMessage(String s);

    public abstract void addFieldError(String s, String s1);

    public abstract boolean hasActionErrors();

    public abstract boolean hasActionMessages();

    public abstract boolean hasErrors();

    public abstract boolean hasFieldErrors();
}

 

 发现public abstract Map getFieldErrors();
是个Map,要把Map转换成list在Map后面加个?keys就可以了,对应页面获取方法:

<#if actionMessages?size gt 0 || fieldErrors?size gt 0>
    <#list actionMessages as message>
  • ${message?default("你请求的页面出错了!")?html?replace("\r\n","
    ")}
  • <#list fieldErrors?keys as field>
  • ${field}:${fieldErrors[field]?default("你请求的页面出错了!")?html?replace("\r\n","
    ")}

 

你可能感兴趣的:(freemarker,Flash,HTML)