我正在尝试将对象类型“用户”插入数据库,但出现此错误:
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" />