2013-01-11 45 views
0

我有三個字段department_Id,department_Name,department_locationdepartmentForm充當模型對象在這個模型的形式。我有註釋驗證字段。現在,我只想在不同的jsp頁面中使用兩個字段說create.jsp和一個字段在不同的jsp頁面中說getDepartmentById如何在綁定時拒絕來自bean的字段進行驗證?

當我按下提交按鈕create.jsp,驗證正在發生,但提供正確的信息後,其未在此頁面提交原因。

沒有給出一個字段department_Id這是由我的DAO層自動生成。所以,請幫助我,如何拒絕這個值來執行我的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; 
    } 
} 

and my 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> 
+0

@Bedwyr堪有非常看看這個問題IM stucking小問題,但即時通訊不能解決這plz幫助我 – henrycharles

回答

0

什麼錯誤說的是,你錯過了價值departmentId,這並不奇怪,因爲你將它定義爲

@NotEmpty 
@Size(min = 1, max = 20,message="") 

你並不真的需要驗證departmentId如果它是由您的代碼自動生成。你可能應該從DepartmentForm中刪除它,尤其是因爲它不在表單中,或者至少使其成爲可選項。

您可以在業務對象中強制它,但窗體支持對象應反映窗體中的內容。

更新

如果departmentId是一個數據庫生成的ID,你應該把它設置爲你的控制器的InitBinder不允許的:

@InitBinder 
public void initBinder(WebDataBinder binder) { 
    binder.setDisallowedFields(new String[] { "departmentId" }); 
} 
+0

是他們的任何方式不反映或轉義綁定對象中的字段值,因爲我需要該字段在其他jsp頁面 – henrycharles

+0

@henrycharles看到編輯。基本上你需要禁止這個領域,然後你不需要驗證反正afaik。 – soulcheck

相關問題