如何在绑定时拒绝bean中的字段进行验证?

时间:2013-01-11 07:31:17

标签: validation spring-mvc annotations hibernate-validator

我在 department_Id,department_Name,department_location 中有三个字段 departmentForm 充当此模型表单中的模型对象。

我使用注释来验证字段。现在,我想只使用不同jsp页面中的两个字段,例如 create.jsp ,并且不同jsp页面中的一个字段说 getDepartmentById

当我按 create.jsp 的提交按钮时,验证正在进行,但在提供了正确的信息之后,此页面中未提交原因。

没有给我的DAO图层自动生成的一个字段department_Id 。所以,请帮助我,如何拒绝此值来执行我的create.jsp页面,以便在数据库中成功创建部门。

当我打印 BindingResult 对象时,它显示如下:

Field error in object 'departmentForm' on field 'departmentId': rejected value [null]; 
codes [NotEmpty.departmentForm.departmentId,NotEmpty.departmentId,NotEmpty.java.lang.String,NotEmpty]; 
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: 
codes [departmentForm.departmentId,departmentId]; arguments []; 
default message [departmentId],org.hibernate.validator.constraints.NotEmpty.message},
[Ljava.lang.Class;@4fc4a198,[Ljava.lang.Class;@764d2b11]; 
default message [may not be empty]`

这是我在控制器中编码的方式:

@RequestMapping(value = "/createDepartment", method = RequestMethod.POST)
public String createEmployee(@Valid DepartmentForm departmentForm,
        BindingResult bindingResult, Map<String, DepartmentForm> model)
        throws Exception {

    if (bindingResult.hasErrors()) {
        System.out.println(bindingResult);
        bindingResult.reject(departmentForm.getDepartmentId());
        return "departmentForm";
    }
    System.out.println("mr ankur jadiy");

    model.put("departmentForm", departmentForm);
    departmentForm.setUpdateStatus('A');
    if (departmentForm.getUpdateStatus() == 'A') {
        departmentServiceImpl
            .actionDecider(convertDeptFormToDeptBO(departmentForm));

    }
    return "Success";
}

我的DepartmentForm代码如下:

package com.nousinfo.tutorial.model;

import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;

public class DepartmentForm {

    @NotEmpty
    @Size(min = 1, max = 20,message="")
    private String departmentId;
    @NotEmpty
    private String departmentName;

    private String departmentLocation;

    private Character updateStatus;
    public String getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public String getDepartmentLocation() {
        return departmentLocation;
    }

    public void setDepartmentLocation(String departmentLocation) {
        this.departmentLocation = departmentLocation;
    }

    public Character getUpdateStatus() {
        return updateStatus;
    }

    public void setUpdateStatus(Character updateStatus) {
        this.updateStatus = updateStatus;
    }
}

我的create.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://jakarta.apache.org/taglibs/input-1.0" prefix="input"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Create Department</title>
<link rel="stylesheet" href="css/style.css" type="text/css"></link>
</head>

<body>

    <table width="1254" height="74" border="0" align="center">

        <tr>
            <td width="300" height="68" align="center" bgcolor="#99CCFF"><h2>
                    <span class="style1">Employee Details </span>
                </h2></td>
            <td width="100" height="68" align="center" bgcolor="#FFFFFF"><img
                src="./image/emps.jpg" width="190" height="92" /></td>
        </tr>
    </table>
    <p>
        <br />
    </p>
    <hr size="1" width="786">
    <form:form id="form" method="post" action="/EmployeeWebSpring/departmentController/createDepartment"
        modelAttribute="departmentForm">
        <table>
            <tr>
                <form:hidden path="updateStatus" />
            </tr>

            <tr>
                <td>
                    Department_Name:
                    <font color="red"><form:errors path="departmentName" /></font>
                </td>
            </tr>
            <tr>
                <td><form:input path="departmentName" /></td>
            </tr>

            <tr>
                <td>
                    Department_Location:
                    <font color="red"><form:errors path="departmentLocation" /></font>
                </td>
            </tr>
            <tr>
                <td><form:input path="departmentLocation" /></td>
            </tr>
        </table>

        <br>
        <br />
        <p>&nbsp;</p>
        <br>
        <tr>
            <td><input type="submit" name="method" value="save" /></td>
            <td><input type="submit" name="method" value="cancel" /></td>
        </tr>

        <hr size="1" width="786">
        <p>&nbsp;</p>
    </form:form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

错误说明的是您错过了departmentId的价值,因为您将其定义为

,这并不奇怪
@NotEmpty
@Size(min = 1, max = 20,message="")

如果您的代码自动生成departmentId,则无需验证DepartmentForm。你可能应该从departmentId删除它,特别是因为它不在表单中,或者至少使它成为可选的。

您可以在业务对象中强制使用它,但表单支持对象应该反映表单中的内容。

<强>更新

如果InitBinder是数据库生成的ID,则应在控制器@InitBinder public void initBinder(WebDataBinder binder) { binder.setDisallowedFields(new String[] { "departmentId" }); } 中将其设置为不允许:

{{1}}