java - jQuery inside SpriingMVC Form -
i've got trouble creating form using springmvc jquery;
here index.jsp:
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@taglib prefix="sec" uri="http://www.springframework.org/security/tags"%> <%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <title>title page</title> <link type="text/css" rel="stylesheet" href="../css/style.css"></link> <script type="text/javascript" src="../js/jquery-2.1.1.js"></script> <script type="text/javascript"> $(document).ready(function(){ $.ajax({ datatype:'json', url:"http://localhost:8080/elections/elections/hello", success: function(jsondata){ $.each(jsondata, function(key,value){ $('.notloggedinresults').append("<p>" + value.name + " has " + value.voices + " voices</p> "); }); } }); }); </script> <sec:authorize access="hasrole('role_user')"> <script> $(document).ready(function(){ $.ajax({ datatype:'json', url:"http://localhost:8080/elections/elections/hello", success: function(jsondata){ $.each(jsondata, function(key,value){ $('.loggedinform #form').append("<form:radiobutton path='name' value=" + value.name + "></form:radiobutton>" + value.name); }); $('.loggedinform #form').append("<input type='submit' value='vote!'</input>" ); } }); $('.notloggedinresults').css('display', 'none'); $('.loggedinform').css('display', 'block'); $('.loggedinform #form').css('border', '1px solid red'); $('.loginsection a').css('display', 'none'); }); </script> </sec:authorize> </head> <body> <body> <div class="header"> </div> <div class="mainsection"> <div class="loginsection"> <p>login section</p> <a href="login">login</a> </div> <div class="resultsection"> <div class="notloggedinresults"> </div> <div class="loggedinform"> <form:form id = "form" method="post" commandname="object" action="process"> </form:form> </div> </div> <div class="infosection"> <p>info section</p> <p>this demo voting application</p> </div> </div> <div class="footer"> </div> </body> </body> </html>
here i'm trying populate form radiobuttons.
if insert tag inside html ok. if use jquery above
i'm getting error:
org.apache.jasper.jasperexception: exception occurred processing jsp page /web-inf/jsps/index.jsp @ line 38 35: success: function(jsondata){ 36: $.each(jsondata, function(key,value){ 37: 38: $('.loggedinform #form').append("<form:radiobutton path='name' value=" + value.name + "></form:radiobutton>" + value.name); 39: }); 40: $('.loggedinform #form').append("<input type='submit' value='vote!'</input>" ); 41: } stacktrace: org.apache.jasper.servlet.jspservletwrapper.handlejspexception(jspservletwrapper.java:568) org.apache.jasper.servlet.jspservletwrapper.service(jspservletwrapper.java:465) org.apache.jasper.servlet.jspservlet.servicejspfile(jspservlet.java:390) org.apache.jasper.servlet.jspservlet.service(jspservlet.java:334) javax.servlet.http.httpservlet.service(httpservlet.java:727) org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) org.springframework.web.servlet.view.internalresourceview.rendermergedoutputmodel(internalresourceview.java:238) org.springframework.web.servlet.view.abstractview.render(abstractview.java:264) org.springframework.web.servlet.dispatcherservlet.render(dispatcherservlet.java:1208) org.springframework.web.servlet.dispatcherservlet.processdispatchresult(dispatcherservlet.java:992) org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:939) org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:856) org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:953) org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:844) javax.servlet.http.httpservlet.service(httpservlet.java:620) org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:829) javax.servlet.http.httpservlet.service(httpservlet.java:727) org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:330) org.springframework.security.web.access.intercept.filtersecurityinterceptor.invoke(filtersecurityinterceptor.java:118) org.springframework.security.web.access.intercept.filtersecurityinterceptor.dofilter(filtersecurityinterceptor.java:84) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.access.exceptiontranslationfilter.dofilter(exceptiontranslationfilter.java:113) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.session.sessionmanagementfilter.dofilter(sessionmanagementfilter.java:103) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.authentication.anonymousauthenticationfilter.dofilter(anonymousauthenticationfilter.java:113) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.servletapi.securitycontextholderawarerequestfilter.dofilter(securitycontextholderawarerequestfilter.java:154) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.savedrequest.requestcacheawarefilter.dofilter(requestcacheawarefilter.java:45) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.authentication.www.basicauthenticationfilter.dofilter(basicauthenticationfilter.java:150) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.authentication.abstractauthenticationprocessingfilter.dofilter(abstractauthenticationprocessingfilter.java:199) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.authentication.logout.logoutfilter.dofilter(logoutfilter.java:110) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.context.request.async.webasyncmanagerintegrationfilter.dofilterinternal(webasyncmanagerintegrationfilter.java:50) org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.context.securitycontextpersistencefilter.dofilter(securitycontextpersistencefilter.java:87) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.filterchainproxy.dofilterinternal(filterchainproxy.java:192) org.springframework.security.web.filterchainproxy.dofilter(filterchainproxy.java:160) org.springframework.web.filter.delegatingfilterproxy.invokedelegate(delegatingfilterproxy.java:343) org.springframework.web.filter.delegatingfilterproxy.dofilter(delegatingfilterproxy.java:260) root cause java.lang.illegalstateexception: neither bindingresult nor plain target object bean name 'name' available request attribute org.springframework.web.servlet.support.bindstatus.<init>(bindstatus.java:144) org.springframework.web.servlet.tags.form.abstractdataboundformelementtag.getbindstatus(abstractdataboundformelementtag.java:179) org.springframework.web.servlet.tags.form.abstractdataboundformelementtag.getpropertypath(abstractdataboundformelementtag.java:199) org.springframework.web.servlet.tags.form.abstractdataboundformelementtag.getname(abstractdataboundformelementtag.java:165) org.springframework.web.servlet.tags.form.abstractdataboundformelementtag.autogenerateid(abstractdataboundformelementtag.java:152) org.springframework.web.servlet.tags.form.abstractcheckedelementtag.autogenerateid(abstractcheckedelementtag.java:81) org.springframework.web.servlet.tags.form.abstractdataboundformelementtag.resolveid(abstractdataboundformelementtag.java:143) org.springframework.web.servlet.tags.form.abstractsinglecheckedelementtag.writetagcontent(abstractsinglecheckedelementtag.java:83) org.springframework.web.servlet.tags.form.abstractformtag.dostarttaginternal(abstractformtag.java:103) org.springframework.web.servlet.tags.requestcontextawaretag.dostarttag(requestcontextawaretag.java:80) org.apache.jsp.web_002dinf.jsps.index_jsp._jspx_meth_form_005fradiobutton_005f0(index_jsp.java:222) org.apache.jsp.web_002dinf.jsps.index_jsp._jspx_meth_sec_005fauthorize_005f0(index_jsp.java:183) org.apache.jsp.web_002dinf.jsps.index_jsp._jspservice(index_jsp.java:100) org.apache.jasper.runtime.httpjspbase.service(httpjspbase.java:70) javax.servlet.http.httpservlet.service(httpservlet.java:727) org.apache.jasper.servlet.jspservletwrapper.service(jspservletwrapper.java:432) org.apache.jasper.servlet.jspservlet.servicejspfile(jspservlet.java:390) org.apache.jasper.servlet.jspservlet.service(jspservlet.java:334) javax.servlet.http.httpservlet.service(httpservlet.java:727) org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) org.springframework.web.servlet.view.internalresourceview.rendermergedoutputmodel(internalresourceview.java:238) org.springframework.web.servlet.view.abstractview.render(abstractview.java:264) org.springframework.web.servlet.dispatcherservlet.render(dispatcherservlet.java:1208) org.springframework.web.servlet.dispatcherservlet.processdispatchresult(dispatcherservlet.java:992) org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:939) org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:856) org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:953) org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:844) javax.servlet.http.httpservlet.service(httpservlet.java:620) org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:829) javax.servlet.http.httpservlet.service(httpservlet.java:727) org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:330) org.springframework.security.web.access.intercept.filtersecurityinterceptor.invoke(filtersecurityinterceptor.java:118) org.springframework.security.web.access.intercept.filtersecurityinterceptor.dofilter(filtersecurityinterceptor.java:84) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.access.exceptiontranslationfilter.dofilter(exceptiontranslationfilter.java:113) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.session.sessionmanagementfilter.dofilter(sessionmanagementfilter.java:103) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.authentication.anonymousauthenticationfilter.dofilter(anonymousauthenticationfilter.java:113) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.servletapi.securitycontextholderawarerequestfilter.dofilter(securitycontextholderawarerequestfilter.java:154) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.savedrequest.requestcacheawarefilter.dofilter(requestcacheawarefilter.java:45) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.authentication.www.basicauthenticationfilter.dofilter(basicauthenticationfilter.java:150) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.authentication.abstractauthenticationprocessingfilter.dofilter(abstractauthenticationprocessingfilter.java:199) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.authentication.logout.logoutfilter.dofilter(logoutfilter.java:110) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.context.request.async.webasyncmanagerintegrationfilter.dofilterinternal(webasyncmanagerintegrationfilter.java:50) org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.context.securitycontextpersistencefilter.dofilter(securitycontextpersistencefilter.java:87) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.filterchainproxy.dofilterinternal(filterchainproxy.java:192) org.springframework.security.web.filterchainproxy.dofilter(filterchainproxy.java:160) org.springframework.web.filter.delegatingfilterproxy.invokedelegate(delegatingfilterproxy.java:343) org.springframework.web.filter.delegatingfilterproxy.dofilter(delegatingfilterproxy.java:260)
please understand why search bean "name" instead of "object".
here contoller:
@controller public class maincontroller { @autowired private applicationcontext context; @requestmapping(value="hello") public @responsebody list<candidate> resultlist(){ electionsdao dao = context.getbean("electiondatasource", jdbcelectionsdao.class); return dao.getallcandidates(); } @requestmapping(value="get") public string getjson(){ return "get"; } @requestmapping(value="register") public string register(){ return "register"; } @requestmapping(value="/") public string mainpage(model model){ model.addattribute("object", new votingobject()); return "index"; } @requestmapping(value="login") public string login(){ return "login"; } @requestmapping(value="/success") public string successredirect(model model){ model.addattribute("object", new votingobject()); return "index"; } @requestmapping(value="/process", method=requestmethod.post) public string method(model model, @modelattribute("object") votingobject object, bindingresult result){ system.out.println(object.getname()); electionsdao dao = context.getbean("electiondatasource", jdbcelectionsdao.class); dao.insertthevote(object.getname()); return "redirect:/elections/"; } }
here dao bean:
public class jdbcelectionsdao implements electionsdao { @autowired private datasource datasource; public list<candidate> getallcandidates() { list<candidate> candidatelist = new arraylist<candidate>(); connection conn = null; string sql = "select * candidates"; try { conn = datasource.getconnection(); statement statement = conn.createstatement(); resultset rs = statement.executequery(sql); while(rs.next()){ candidate candidate = new candidate(); candidate.setname(rs.getstring(2)); candidate.setvoices(rs.getint(3)); candidatelist.add(candidate); } } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } return candidatelist; } public void insertthevote(string name) { // todo auto-generated method stub string sql = "update candidates set voices = voices+1 name= ?"; connection conn = null; try { conn=datasource.getconnection(); preparedstatement stmt = conn.preparestatement(sql); stmt.setstring(1, name); stmt.execute(); stmt.close(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } finally{ try { conn.close(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } } public int registeruser(elector user) { // todo auto-generated method stub string sql = "insert users values(?,?,?)"; connection conn = null; try { conn=datasource.getconnection(); preparedstatement stmt = conn.preparestatement(sql); stmt.setstring(1, user.getname()); stmt.setstring(2, user.getlogin()); stmt.setstring(3, user.getpassword()); stmt.setboolean(4, user.isvoted()); stmt.close(); return stmt.getupdatecount(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); }finally{ try { conn.close(); } catch (sqlexception e) { // todo auto-generated catch block e.printstacktrace(); } } return 0; } public datasource getdatasource() { return datasource; } public void setdatasource(datasource datasource) { this.datasource = datasource; } }
context.xml
<bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="com.mysql.jdbc.driver" /> <property name="url" value="jdbc:mysql://localhost:3306/elections" /> <property name="username" value="root" /> <property name="password" value="komato3" /> </bean> <bean id ="electiondatasource" class="ua.macko.dao.impl.jdbcelectionsdao" > <property name="datasource" ref="datasource"/> </bean>
imagine there list of products want display, :
in below form, can see, checking listproducts, attribute have added in controller using modelattribute. show how in controller code. jsp page :
<h3>product list</h3> <c:if test="${!empty listproducts}"> <table class="tg"> <tr> <th width="80">product id</th> <th width="120">product name</th> <th width="120">product description</th> </tr> <c:foreach items="${listproducts}" var="product"> <tr> <td>${product.productid}</td> <td>${product.productname}</td> <td>${product.productdescription}</td> </tr> </table> </c:if>
controller :
@requestmapping(value="/product/show",method= requestmethod.get) public string listproducts(model model){ user user = userservice.getcurrentlyauthenticateduser(); model.addattribute("product", new productbasic()); model.addattribute("listproducts",this.productbasicservice.listproduct(user)); }
servlet-context.xml :
if incase using hibernate, use such beans tied session factory, or else skip sesionfactory part.
<beans:bean id="canvasdao" class="com.journaldev.spring.dao.canvasdaoimpl"> <beans:property name="sessionfactory" ref="hibernate4annotatedsessionfactory"/> </beans:bean> <beans:bean id="canvasservice" class="com.journaldev.spring.service.canvasserviceimpl"> <beans:property name="canvasdao" ref="canvasdao"/> </beans:bean>
as can see, calling listproduct returns list, , displaying on form...my model contains productid, productname, productdescription. if mistake change productname productxyz, error seeing. also, can post bean code related service,dao model?
Comments
Post a Comment