0

我正在尝试将对象类型“用户”插入数据库,但出现此错误:

org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/pages/gestionUser.jsp at line 165

162:                        
163:                <tr> 
164:                    <td id="idtab">Nom :</td> 
165:                     <td><form:input type="text" path="nom" class="round default-width-input"/></td>
166:                     <td><form:errors path="nom" cssClass="errorbox" /></td>                        
167:                </tr>
168:                <tr>            

异常是: java.lang.IllegalStateException:Bean 名称“用户”的 BindingResult 和普通目标对象都不能用作请求属性

这是我的表格:

<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>
                        </div> <!-- end content-module-main -->

                    </div> <!-- end content-module -->

这是控制器中实现 SimpleFormController 的方法

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

    }

请帮忙 ?错误在哪里?谢谢


这是我的控制器:

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.ModelAttribute;
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)
    @ModelAttribute("user")
    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";

    }
}

这是用户类

    package gestion.delegation.domaine;

    public class User {
        int id;
        String nom;
        String prenom;
        String login;
        String password;
        String role;
        boolean enable;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getNom() {
            return nom;
        }
        public void setNom(String nom) {
            this.nom = nom;
        }
        public String getPrenom() {
            return prenom;
        }
        public void setPrenom(String prenom) {
            this.prenom = prenom;
        }
        public String getLogin() {
            return login;
        }
        public void setLogin(String login) {
            this.login = login;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public boolean getEnable() {
            return this.enable;
        }
        public void setEnable(boolean enable) {
            this.enable = enable;
        }

        public User(int id, String 

nom, String prenom, String login,
            String password, String role, boolean enable) {
        super();
        this.id = id;
        this.nom = nom;
        this.prenom = prenom;
        this.login = login;
        this.password = password;
        this.role = role;
        this.enable = enable;
    }
    public String getRole() {
        return role;
    }
    public void setRole(String role) {
        this.role = role;
    }
    public User() {
        super();
    }

}

验证器:

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");

    }

}

这是 Dispatcher-servlet.xml 中验证器和控制器包的声明

 <bean id="AddUserValidator" class="gestion.delegation.validator.AddUserValidator" />


     <context:component-scan base-package="gestion.delegation.controller" />
4

2 回答 2

0

好的,我将假设您的模型中确实有一个用户。因此,为了将其发布回您的控制器,您可以使用相同的注释并将其作为方法中的参数之一。提交表单后,Spring 会将其绑定到 User 参数:

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

    }
于 2013-04-30T20:27:21.533 回答
0

您需要将用户 bean 放入模型中。由于 commandName="user" 属性,表单需要它。

一种方法是使用modelAttribute注释

像这样的东西

@ModelAttribute
public User user(){
    return new User();
}
于 2013-04-30T19:44:23.353 回答