0

我想在 spring 方面进行验证.. Spring 验证,但是当我试图在表单上传递错误时,我得到了错误

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'newProductType' available as request attribute
    org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:164)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:151)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:142)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:126)
    org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421)
    org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
    org.apache.jsp.jsp.admin_jsp._jspx_meth_form_005finput_005f0(admin_jsp.java:532)
    org.apache.jsp.jsp.admin_jsp._jspService(admin_jsp.java:143)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1157)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:927)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

这是我的代码:

验证器: 公共类 MetalTypeValidator 实现验证器 {

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

@Override
public void validate(Object target, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "value", "value.required");
}

}

在 spring 的 xml 中声明验证器:

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="metalTypeValidator" class="com.javanix.xmetal.web.validator.MetalTypeValidator" />

控制器:

@Controller
@RequestMapping("/admin")
public class AdminController implements IConstants {
    @Autowired
    private MetalTypeValidator metalTypeValidator;

    @Autowired
    private MetalTypeServiceImpl metalTypeService;

    @RequestMapping
    public String getMainPageData(Map<String, Object> map) {
        map.put("newProductType", new MetalTypeEntity());
        map.put("productTypeList", metalTypeService.getList());
        return "admin";
    }

    @RequestMapping(value = "/add/product_type", method = RequestMethod.POST)
    public String addProductType(
            @Valid @ModelAttribute("newType") MetalTypeEntity entity,
            BindingResult result, SessionStatus status) {

        metalTypeValidator.validate(entity, result);
        if (result.hasErrors()) {
            // result.getModel().put("newProductType", new MetalTypeEntity());
            return "/admin";
        }
        metalTypeService.add(entity);
        return "redirect:/admin";
    }

}

瓷砖.xml

<definition name="admin" extends=".mainTemplate" >
    <put-attribute name="title" value="Admin page" type="string" />
    <put-attribute name="content" value="/jsp/admin.jsp" />
    <!-- here some other stuff -->
</definition>

admin.jsp(使用我的实体的块):

<div id="MetalType">
    <table class="data">
        <tr>
            <th><spring:message code="label.header.metal.name"/> **</th>
            <th>&nbsp;</th>
        </tr>
        <c:forEach items="${productTypeList}" var="productType">
            <tr>
                <td>${productType.value}</td>
                <td><a href = "<c:url value="admin/delete/product_type/${productType.id}" />" > <spring:message code="label.button.delete"/> </a> </td>
            </tr>
        </c:forEach>

        <form:form method="post" action="admin/add/product_type" commandName="newProductType">
            <tr>
                <td><form:input path="value"/></td>
                <td><input type="submit" value="<spring:message code="label.type.add"/>"/></td>
            </tr>
            <tr>
                <td><form:errors path="value" cssClass="errors"/></td>
                <td>&nbsp;</td>
            </tr>
        </form:form>

    </table>
</div>
4

2 回答 2

1

将这样的内容添加到您的Controller课程中:

@ModelAttribute("newProductType")
public MetalTypeEntity createMetalTypeEntity() {
    return new MetalTypeEntity();
}
于 2012-06-08T12:01:36.813 回答
1

这是 Spring CRUD 应用程序的一个很好的示例:

http://www.javacodegeeks.com/2011/04/spring-mvc3-hibernate-crud-sample.html?m=1

看一下

@RequestMapping(value="/updateContact", method=RequestMethod.POST)
 public String update(@ModelAttribute("editContact") Contact contact, BindingResult result, SessionStatus status)
 {
  validator.validate(contact, result);
  if (result.hasErrors()) {
   return "editContact";
  }
  contactsDAO.update(contact);
  status.setComplete();
  return "redirect:viewAllContacts.do";
 }
于 2012-06-08T13:27:51.857 回答