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";
}
}
tiles.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> </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> </td>
</tr>
</form:form>
</table>
</div>