2013-04-30 94 views
0

我想添加一個用戶到我的數據庫,所以我創建一個驗證器的形式,但這一個不工作,當我留下一些領域空,我點擊按鈕提交什麼都沒發生我只是有這個錯誤:驗證器不工作 - 春天MVC 3

Etat HTTP 500 - Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [insert into utilisateurs (id,login, password, nom, prenom,enable) values (?,?,?,?,?,?)]; Column 'login' cannot be null; nested exception is com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'login' cannot be null 

這是形式:

<form:form action="${pageContext.request.contextPath}/ajouter_user" method="post" commandName="user"> 

      <table id="tabmenu"> 


     <tr> 
      <td id="idtab">Nom :</td> 
      <td><form:input type="text" path="nom" class="round default-width-input"/></td> 
      <td><form:errors path="nom" cssClass="errorbox" /></td>       
     </tr> 
     <tr>    
     <td id="idtab">Prénom :</td> 
     <td> <form:input type="text" path="prenom" class="round default-width-input"/></td> 
     <td><form:errors path="prenom" cssClass="errorbox" /></td> 
     </tr> 
     <tr> 
     <td id="idtab">Login :</td> 
     <td> <form:input type="text" path="login" cssClass="round default-width-input"/></td> 
     <td><form:errors path="login" class="errorbox" /></td> 
     <tr> 
     <td id="idtab">Password :</td> 
     <td> <form:input type="password" path="password" class="round default-width-input"/></td> 
     <td><form:errors path="password" cssClass="errorbox" /></td>  
     </tr> 
     <tr> 
    <td id="idtab">Séléctionner un rôle :</td> 
    <td> <form:select path="role"> 
         <form:option value="" label="Selectionner" /> 
         <form:option value="1">Administrateur</form:option> 
         <form:option value="2">Simple utilisateur</form:option> 
     </form:select></td> 
    <td><form:errors path="role" cssClass="errorbox" /></td>  
     </tr> 
     <tr> 
     <td id="idtab">Désactivé :</td> 
     <td><form:input type="checkbox" value="true" checked="checked" path="enable"/> Oui</td>  
    </tr> 
    <tr></tr> 
    <tr></tr> 
       <tr> <td colspan=2><input class="button round blue image-right ic-right-arrow" type="submit" value="Créer"></td></tr> 

      </table> 
     </form:form> 
      <div class="success"><c:out value="${msg_success}" /></div> 

的驗證:

package gestion.delegation.validator; 

import gestion.delegation.domaine.User; 

import org.springframework.validation.Errors; 
import org.springframework.validation.ValidationUtils; 
import org.springframework.validation.Validator; 

public class AddUserValidator implements Validator{ 

    @Override 
    public boolean supports(Class<?> clazz) { 

     return User.class.isAssignableFrom(clazz); 
    } 

    @Override 
    public void validate(Object obj, Errors err) { 
     ValidationUtils.rejectIfEmptyOrWhitespace(err, "nom", "name.required","Choisissez un nom"); 
     ValidationUtils.rejectIfEmptyOrWhitespace(err, "prenom", "prenom.required", "Choisissez un prenom"); 
     ValidationUtils.rejectIfEmptyOrWhitespace(err, "login", "login.required", "Choisissez un login"); 
     ValidationUtils.rejectIfEmptyOrWhitespace(err, "password", "password.required", "Choisissez un password"); 
     ValidationUtils.rejectIfEmpty(err, "role", "role.required", "Choisissez un role"); 

    } 

} 

,這是控制器:

package gestion.delegation.controller; 

import gestion.delegation.domaine.User; 
import gestion.delegation.service.ImplIUserService; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 




@Controller 
public class GestionUserController{ 

    private ImplIUserService userservice; 

    @Autowired 
    public void setImplserv(ImplIUserService userservice) { 
     this.userservice = userservice; 
    } 


    @RequestMapping(value = "/ajouter_user", method = RequestMethod.POST) 
    public String add(ModelMap model) { 
     User user = new User(); 
     userservice.AddUser(user); 
     String msg= "Vous avez ajouter un utilisateur avec succès !"; 
     model.addAttribute("msg_success",msg); 
     return "gestionUser"; 

    } 
} 




public void AddUser(User user) { 

     final String User_INSERT1 = "insert into utilisateurs (id,login, password, nom, prenom,enable) " 
       + "values (?,?,?,?,?,?)"; 
     final String User_INSERT2="insert into roles (id,login,role) values(?,?,?)"; 
     /* 
     * On récupère et on utilisera directement le jdbcTemplate 
     */ 
     getJdbcTemplate() 
       .update(User_INSERT1, 
         new Object[] { user.getId(), user.getLogin(), 
           user.getPassword(), user.getNom(), 
           user.getPrenom(), user.getEnable(), 
           }); 
     getJdbcTemplate().update(User_INSERT2, new Object[]{ user.getId(),user.getLogin(),user.getRole()}); 
    } 

這表明我的錯誤是在這裏:.update(User_INSERT1, 那麼,是錯在這裏?請幫忙 !謝謝

回答

0

你add方法通過「用戶」對象的「UserService.AddUser」的方法,但「用戶」對象基本上是空的 - 它只是對代碼:)

User user = new User(); 
userservice.AddUser(user); 
前行創建
+0

但我認爲窗體必須設置User對象的字段值? – Somar 2013-04-30 23:23:07

+0

我解決了它是驗證器的問題,我沒有在控制器類中創建它的實例。謝謝 – Somar 2013-05-01 00:39:38