java - add user using spring mvc framework -
hello want save user database using spring mvc framework got error.
java.lang.illegalstateexception: neither bindingresult nor plain target object bean name 'command' available request attribute @ org.springframework.web.servlet.support.bindstatus.<init> (bindstatus.java:144) atorg.springframework.web.servlet.tags.form.abstractdataboundformelementtag. getbindstatus(abstractdataboundformelementtag.java:168)at org.springframework.web.servlet.tags.form.abstractdataboundformelementtag. getpropertypath(abstractdataboundformelementtag.java:188) @ org.springframework.web.servlet.tags.form.abstractdataboundformelementtag .getname(abstractdataboundformelementtag.java:154) @ org.springframework.web.servlet.tags.form.abstractdataboundformelementtag .autogenerateid(abstractdataboundformelementtag.java:141) atorg.springframework.web.servlet.tags.form.abstractdataboundformelementtag. resolveid(abstractdataboundformelementtag.java:132) atorg.springframework.web.servlet.tags.form. abstractdataboundformelementtag.writedefaultattributes (abstractdataboundformelementtag.java:116) @ org.springframework.web.servlet.tags.form.abstracthtmlelementtag .writedefaultattributes(abstracthtmlelementtag.java:422) @ org.springframework.web.servlet.tags.form.inputtag .writetagcontent(inputtag.java:142) @ org.springframework.web.servlet.tags.form.abstractformtag .dostarttaginternal(abstractformtag.java:84) @ org.springframework.web.servlet.tags .requestcontextawaretag.dostarttag(requestcontextawaretag.java:80) @ org.apache.jsp.views.users.adduser_jsp._jspx_meth_form_005finput_005f0 (adduser_jsp.java:522) @ org.apache.jsp.views.users.adduser_jsp._jspservice(adduser_jsp.java:219) @ org.apache.jasper.runtime.httpjspbase.service(httpjspbase.java:70)
genericdao
package com.ossn.api; import java.util.list; public interface genericdao<e, id> { public e create(e entity); }
genericdaoimpl
com.ossn.api; import java.io.serializable; import java.lang.reflect.parameterizedtype; import java.util.list; import javax.persistence.entitymanager; import javax.persistence.persistencecontext; import javax.persistence.criteria.criteriabuilder; import javax.persistence.criteria.criteriaquery; import javax.persistence.criteria.root; import org.springframework.transaction.annotation.transactional; public class genericdaojpaimpl<e, id extends serializable> implements genericdao<e, id> { protected class<e> modelclass; @persistencecontext private entitymanager entitymanager; public genericdaojpaimpl() { parameterizedtype genericsuperclass = (parameterizedtype) getclass().getgenericsuperclass(); this.modelclass = (class<e>) genericsuperclass.getactualtypearguments()[0]; } @override public e create(e entity) { entitymanager em = getentitymanager(); em.persist(entity); return entity; } }
generic service
package com.ossn.api; import java.util.list; public interface genericservice<e, id> { public e create(e entity); }
genericserviceimpl
package com.ossn.api; import java.util.list; import org.springframework.transaction.annotation.transactional; public abstract class genericserviceimpl<e, k> implements genericservice<e, k> { private genericdao<e, k> dao; public genericserviceimpl(genericdao<e, k> dao) { this.dao = dao; } public genericserviceimpl() { } @transactional public e create(e entity) { return dao.create(entity); } }
la model.user
package com.ossn.model; import java.io.serializable; import javax.persistence.*; import org.hibernate.validator.constraints.notempty; /** * persistent class users database table. * */ @entity @table(name="users") @namedquery(name="user.findall", query="select u user u") public class user implements serializable { private static final long serialversionuid = 1l; @id @generatedvalue(strategy = generationtype.identity) private long id; @column(name="email") private string email; @notempty @column(name="name") private string name; @column(name="password") private string password; @column(name="surname") private string surname; @column(name="username") private string username; public user() { } public long getid() { return this.id; } public void setid(long id) { this.id = id; } public string getemail() { return this.email; } public void setemail(string email) { this.email = email; } public string getname() { return this.name; } public void setname(string name) { this.name = name; } public string getpassword() { return this.password; } public void setpassword(string password) { this.password = password; } public string getsurname() { return this.surname; } public void setsurname(string surname) { this.surname = surname; } public string getusername() { return this.username; } public void setusername(string username) { this.username = username; } }
userdaoimpl class
package com.ossn.dao.impl; import java.util.date; import java.util.list; import java.util.map; import javax.persistence.entitymanager; import javax.persistence.persistencecontext; import javax.persistence.query; import org.springframework.stereotype.repository; import com.ossn.api.genericdaojpaimpl; import com.ossn.dao.userdao; import com.ossn.model.user; @repository public class userdaoimpl extends genericdaojpaimpl<user, long> implements userdao { @persistencecontext entitymanager em; private class<user> type; }
userservice class package com.ossn.service;
import java.util.date; import java.util.list; import java.util.map; import com.ossn.api.genericservice; import com.ossn.model.user; public interface userservice extends genericservice<user, long> { }
userserviceimpl
package com.ossn.service.impl; import java.util.date; import java.util.list; import java.util.map; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.qualifier; import org.springframework.stereotype.service; import com.ossn.api.genericdao; import com.ossn.api.genericserviceimpl; import com.ossn.dao.userdao; import com.ossn.model.user; import com.ossn.service.userservice; @service public class userserviceimpl extends genericserviceimpl<user, long> implements userservice { @autowired public userserviceimpl(@qualifier("userdaoimpl") genericdao<user, long> dao) { super(dao); } @autowired private userdao dao; }
usercontroller
package com.ossn.spring.controller; import java.util.arraylist; import java.util.list; import javax.servlet.http.httpservletrequest; import javax.validation.valid; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.ui.model; import org.springframework.ui.modelmap; import org.springframework.validation.bindingresult; import org.springframework.web.bind.annotation.modelattribute; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import com.ossn.model.user; import com.ossn.service.userservice; @controller @requestmapping(value="/") public class usercontroller { @autowired userservice userservice; /* @requestmapping(method = requestmethod.get) public string afficher(modelmap pmodel) { final list<user> llisteusers = userservice.findall(); pmodel.addattribute("listeusers", llisteusers); return "users/user"; }*/ @requestmapping(value="/",method=requestmethod.get) public string userurl(final modelmap pmodel){ if(pmodel.get("user")==null){ pmodel.addattribute("user",new user()); } return "users/adduser"; } @requestmapping(value="/add" ,method=requestmethod.post) public string saveuser(@valid @modelattribute("user")user user, bindingresult pbindingresult, modelmap pmodel){ if (!pbindingresult.haserrors()) { this.userservice.create(user); } return "/users/user"; } }
adduser.jsp
<%@ include file="/views/includes/include.jsp" %>
<!doctype html> <title>inscription</title> <link href="<%=request.getcontextpath() %>/resources/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <!-- metismenu css --> <link href="<%=request.getcontextpath() %>/resources/vendor/metismenu/metismenu.min.css" rel="stylesheet"> <!-- custom css --> <link href="<%=request.getcontextpath() %>/resources/dist/css/sb-admin- 2.css" rel="stylesheet"> <!-- custom fonts --> <link href="<%=request.getcontextpath() %>/resources/vendor/font- awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css"> <body> <!-- page content --> <div id="page-wrapper"> <div class="container-fluid"> <div class="row"> <div class="col-lg-12"> <h1 class="page-header"> <fmt:message key="user.new" /> </h1> </div> <!-- /.col-lg-12 --> </div> <div class="row"> <div class="col-lg-12"> <div class="panel panel-success"> <div class="panel-heading"> <fmt:message key="user.new" /> </div> <!-- /.panel-heading --> <div class="panel-body"> <form:form method="post" modelatrribute="user" action="add" > <div class="form-group"> <label><fmt:message key="common.nom"></fmt:message></label> <form:input path="name" class="form-control" /> <b><i><form:errors path="name" cssclass="error"/></i></b><br> <p class="help-block"></p> </div> <div class="form-group"> <label><fmt:message key="common.prenom"></fmt:message></label> <form:input path="surname" class="form-control"/> <!-- --> <p class="help-block"></p> </div> <div class="form-group"> <label><fmt:message key="common.motsdepasse"></fmt:message></label> <form:input path="password" class="form-control"/> <p class="help-block"></p> </div> <div class="form-group"> <label><fmt:message key="common.nomutilisateur"></fmt:message></label> <form:input path="username" class="form-control"/> <p class="help-block"></p> </div> <div class="form-group"> <label><fmt:message key="common.email"></fmt:message></label> <form:input path="email" class="form-control"/> <p class="help-block"></p> </div> <div class=panel-footer> <button type="submit" class="btn btn-success"><fmt:message key="common.enregistrer"></fmt:message></button> <a href="<c:url value="/" />" class="btn btn-danger"> <i class="fa fa- arrow-left"> <fmt:message key="common.annuler" /></i></a> </div> </form:form> </div> <!-- /.panel-body --> </div> <!-- /.panel --> </div> <!-- /.col-lg-12 --> </div> <!-- /.row --> </div> <!-- /.container-fluid --> </div> <!-- /#page-wrapper --> <!-- /#wrapper --> <!-- jquery --> <script src="<%=request.getcontextpath() %>/resources/vendor/jquery/jquery.min.js"></script> <!-- bootstrap core javascript --> <script src="<%=request.getcontextpath() %>/resources/vendor/bootstrap/js/bootstrap.min.js"></script> <!-- metis menu plugin javascript --> <script src="<%=request.getcontextpath() %>/resources/vendor/metismenu/metismenu.min.js"></script> <!-- custom theme javascript --> <script src="<%=request.getcontextpath() %>/resources/dist/js/sb-admin- 2.js"></script> </body>
so not error comes from
Comments
Post a Comment