Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么不';我的JSP页面上显示的Spring验证错误?_Spring_Validation_Jsp_Binding_Message - Fatal编程技术网

为什么不';我的JSP页面上显示的Spring验证错误?

为什么不';我的JSP页面上显示的Spring验证错误?,spring,validation,jsp,binding,message,Spring,Validation,Jsp,Binding,Message,我使用的是Spring3.1.1.RELEASE。我无法在JSP上显示错误消息。这是我在控制器中使用的方法 @RequestMapping(value = "/save", method = RequestMethod.POST) public ModelAndView save(final HttpServletRequest request, final Organization organization,

我使用的是Spring3.1.1.RELEASE。我无法在JSP上显示错误消息。这是我在控制器中使用的方法

@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView save(final HttpServletRequest request,
                         final Organization organization,
                         final Model model,
                         final BindingResult result) 
{
    String nextPage = "organizations/add";
    m_orgValidator.validate(organization, result);
    if (!result.hasErrors())
    {
        final boolean isUpdate = !StringUtils.isEmpty(organization.getId()); 
        // We need this clause to prevent detached entity errors.
        if (StringUtils.isEmpty(organization.getId()))
        {
            organization.setId(null);
        }   // if
        m_orgSvc.save(organization);
        final String msgKey = isUpdate ? "org.updated.successfully" : "org.added.successfully"; 
        final Object[] args = new Object[0];
        request.setAttribute(STATUS, 
                             MessageFormat.format(resourceBundle.getString(msgKey), args));

        nextPage = "landing";
    } else { 
        model.addAttribute(ORG_MODEL_NAME, organization);
    }   // if

    return new ModelAndView(nextPage);
}   // save
这是我的JSP

    <c:url var="action" value="/organizations/save" />
    <form:form modelAttribute="org" method="post" action="${action}">
        <form:hidden path="id" />
        <fieldset>
            <legend>Upload Fields</legend>
            <p><form:errors path="*" cssClass="error" /></p>
            <p>
                <form:label for="name" path="name">Name *</form:label><br/>
                <form:input path="name" type="text"/>
            </p>

上传字段

名称*

我已经通过调试验证了我的验证器是否正确设置了错误(result.hasErrors()=true),但是错误没有正确显示在我的页面上。有什么想法吗?下面是我的验证器类,如果有帮助的话

@Component
public class OrgValidator implements Validator
{

    @Autowired
    private OrganizationService m_orgSvc;

    …

    public boolean supports(Class<?> clazz)
    {
        return Organization.class.isAssignableFrom(clazz);
    }

    public void validate(final Object target,
                         final Errors errors)
    {
        final Organization org = (Organization) target;

        if (org != null && !StringUtils.isEmpty(org.getEodbId()))
        {
            final String eodbId = org.getEodbId();
            final Organization foundOrg = m_orgSvc.findByEodbId(eodbId);
            if ((org.getId() == null && foundOrg != null) ||
                (org.getId() != null && foundOrg != null && !org.getId().equals(foundOrg.getId())))
            {
                errors.rejectValue("eodbId", "AlreadyExists.org.eodb.id");
            }   // if
        } else if (org == null || org != null && StringUtils.isEmpty(org.getEodbId())) { 
            errors.rejectValue("eodbId", "NotExists.org.eodb.id");   
        }   // if

        if (org == null || org.getOrganizationType() == null)
        {
            errors.rejectValue("organizationType", "Invalid.org.type");
        }   // if

        if (org == null || org.getCountry() == null)
        {
            errors.rejectValue("country", "Invalid.org.country");
        }   // if

        if (org == null || org.getState() == null)
        {
            errors.rejectValue("state", "Invalid.org.state");
        }   // if
    }

}
@组件
公共类OrgValidator实现验证器
{
@自动连线
私人组织服务机构;
…
公共布尔支持(类clazz)
{
返回组织.class.isAssignableFrom(clazz);
}
公共无效验证(最终对象目标,
最终错误)
{
最终组织机构=(组织机构)目标;
if(org!=null&&!StringUtils.isEmpty(org.getEodbId())
{
最后一个字符串eodbId=org.getEodbId();
最终组织foundOrg=m_orgSvc.findByEodbId(eodbId);
if((org.getId()==null&&foundOrg!=null)||
(org.getId()!=null&&foundOrg!=null&&org.getId().equals(foundOrg.getId()))
{
errors.rejectValue(“eodbId”、“AlreadyExists.org.eodb.id”);
}//如果
}如果(org==null | | org!=null&&StringUtils.isEmpty(org.getEodbId()){
errors.rejectValue(“eodbId”、“NotExists.org.eodb.id”);
}//如果
if(org==null | | org.getOrganizationType()==null)
{
errors.rejectValue(“organizationType”,“Invalid.org.type”);
}//如果
if(org==null | | org.getCountry()==null)
{
errors.rejectValue(“country”,“Invalid.org.country”);
}//如果
if(org==null | | org.getState()==null)
{
errors.rejectValue(“state”、“Invalid.org.state”);
}//如果
}
}

原因是BindingResult被绑定为一个请求属性,其名称以
.organization
结尾,并尝试使用此后缀检索它,而您的模型属性名称是
org
,这就是它无法找到模型错误的原因

修复方法是命名您的模型属性
organization
,并在jsp中使用此名称:

model.addAttribute("organization", organization);


<form:form modelAttribute="organization" method="post" action="${action}">
        <p><form:errors path="*" cssClass="error" /></p>
model.addAttribute(“组织”,组织);