欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

struts2 Validator 整合freemarker 取得FieldError的方法

程序员文章站 2022-07-14 20:19:37
...

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>
<center>
<div id="actionMessages" class="hightLightDiv">
	<ul>
	<#list actionMessages as message>
		<li>${message?default("你请求的页面出错了!")?html?replace("\r\n","<br>")}</li>
	</#list>
	<#list fieldErrors?keys as field>
		<li>${field}:${fieldErrors[field]?default("你请求的页面出错了!")?html?replace("\r\n","<br>")}</li>
	</#list>
	</ul>
</div>
</center>
</#if>