0

我想将一个用户添加到我的数据库中,因此我创建了一个带有验证器的表单,但是这个表单不起作用,当我将一些字段留空并单击按钮提交时没有发生任何事情我只是有这个错误:

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, 那么这里哪里出错了?请帮助!谢谢

4

1 回答 1

0

您的 add 方法将 'user' 对象传递给 'UserService.AddUser' 方法,但 'user' 对象基本上是空的 - 它只是在前一行代码中创建的 :)

User user = new User();
userservice.AddUser(user);
于 2013-04-30T23:09:36.033 回答