Spring retrieving one user by id -
i have on page list of users retrieved database. along users associated child objects, on same page instead of list of users, 1 user logged in application. restricted viewing of user details , should able view own specific details , child objects.
dao - method getting users
public list<module> getsettermodules(integer userid){ session session = sessionfactory.getcurrentsession(); query query = session.createquery("from userentity u u.userid="+userid); userentity userentity = (userentity) query.uniqueresult(); return new arraylist<module>(userentity.getsmodule());
the dto retrieving list of users , associated objects retrieve 1 user (that authenticated) id , list of objects linked them. i'm attempting implementing this:
@requestmapping(value = "/main", method = requestmethod.get) public string getrecords(@requestparam("userid") integer userid, modelmap model) { //retrieve 1 user userentity user = userservice.getuserbyid(userid); userentitydto userdto = new userentitydto(); userentitydto dto = new userentitydto(); dto.setuserid(user.getuserid()); dto.setsmodule(moduleservice.getsettermodules(user.getuserid())); dto.setcmodule(moduleservice.getcheckermodules(user.getuserid())); userdto.add(dto); } model.addattribute("user", userdto); return "/main";}
i insight how proceed here or there simpler way of getting user.
new dao
@transactional public userentity getuserbyid(integer userid) { session session = sessionfactory.getcurrentsession(); userentity userentity = (userentity) session.get(userentity.class, userid); userentity.getsmodule(); return userentity;}
controller
@requestmapping(value = "/main/user/testing", method = requestmethod.get) public string getrecords(@requestparam("userid") integer userid, modelmap model) { userentity userentity = userservice.getuserbyid(userid); model.addattribute("user", userentity); return "/main/user/testing"; }
error
http status 400 - required integer parameter 'userid' not present
edit 2: above implementation url returning error above tried hardcode id url:
<c:url value="/main/user/testing/?userid=2" var="url"/><a href="<c:out value='${url}'/>">test</a>
and changed controller to:
@requestmapping(value = "/main/user/testing{userid}", method = requestmethod.get) public string getrecords(@requestparam("userid") integer userid, modelmap model) { userentity userentity = userservice.getuserbyid(userid); model.addattribute("user", userentity); return "/main/user/testing"; }
but error now:
javax.servlet.jsp.jsptagexception: don't know how iterate on supplied "items" in <foreach> org.apache.taglibs.standard.tag.common.core.foreachsupport.toforeachiterator(foreachsupport.java:274) org.apache.taglibs.standard.tag.common.core.foreachsupport.supportedtypeforeachiterator(foreachsupport.java:238) org.apache.taglibs.standard.tag.common.core.foreachsupport.prepare(foreachsupport.java:155) javax.servlet.jsp.jstl.core.looptagsupport.dostarttag(looptagsupport.java:256) org.apache.jsp.web_002dinf.views.main.user.testing_jsp._jspx_meth_c_005fforeach_005f0(testing_jsp.java:181) org.apache.jsp.web_002dinf.views.main.user.testing_jsp._jspservice(testing_jsp.java:125) org.apache.jasper.runtime.httpjspbase.service(httpjspbase.java:70) javax.servlet.http.httpservlet.service(httpservlet.java:728) 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:728) 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:915) org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:811) javax.servlet.http.httpservlet.service(httpservlet.java:621) org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:796) javax.servlet.http.httpservlet.service(httpservlet.java:728) 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:54) 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:183) org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) org.springframework.security.web.authentication.logout.logoutfilter.dofilter(logoutfilter.java:105) 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.access.channel.channelprocessingfilter.dofilter(channelprocessingfilter.java:144) 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:346) org.springframework.web.filter.delegatingfilterproxy.dofilter(delegatingfilterproxy.java:259)
this jsp want get:
<table> <tr> <th>user id</th> <th>module code</th> <th>module name</th> </tr> <c:foreach items="${setter}" var="obj" > <c:foreach items="${obj.smodule}" var="module" > <tr> <td><c:out value="${obj.userid}" escapexml="true" /></td> <td><c:out value="${module.modulecode}" escapexml="true" /></td> <td><c:out value="${module.modulename}" escapexml="true" /></td> </tr> </c:foreach> </c:foreach> </table>
i know how user , associated modules , without having hard code userid url. great.
checkout get
method on session
(documentation)
session.get(userentity.class, userid);
Comments
Post a Comment