我目前在一个页面上有我从数据库中检索到的用户列表。与用户一起的是关联的子对象,但我想做的是在同一页面上而不是所有用户的列表上,它只是当前登录到应用程序的一个用户。因此,他们被限制查看所有用户详细信息,并且应该只能查看自己的特定详细信息和子对象。
DAO——获取所有用户的方法
public List<Module> getSetterModules(Integer userId){
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("FROM UserEntity as u WHERE
u.userId="+userId);
UserEntity userEntity = (UserEntity) query.uniqueResult();
return new ArrayList<Module>(userEntity.getsModule());
用于检索用户列表和关联对象的 dto,我只想通过他们的 id 检索一个用户(经过身份验证)并列出链接到他们的所有对象。我正在尝试像这样实现它:
@RequestMapping(value = "/main", method = RequestMethod.GET)
public String getRecords(@RequestParam("userId") Integer userId, ModelMap
model) {
//Retrieve one 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";}
我想了解如何从这里开始,或者是否有更简单的方法来获取用户。
新的 DAO
@Transactional
public UserEntity getUserByID(Integer userId) {
Session session = sessionFactory.getCurrentSession();
UserEntity userEntity = (UserEntity) session.get(UserEntity.class, userId);
userEntity.getsModule();
return userEntity;}
控制器
@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";
}
错误
HTTP Status 400 - Required Integer parameter 'userId' is not present
编辑 2:上面的实现 url 返回上面的错误,所以我尝试将 id 硬编码到 url 中:
<c:url value="/main/user/testing/?userId=2" var="url"/><a href="<c:out
value='${url}'/>">test</a>
并将控制器更改为:
@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";
}
但现在的错误是:
javax.servlet.jsp.JspTagException: Don't know how to iterate over 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)
这是我想要得到的jsp:
<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>
我真的很想知道如何获取用户和相关模块,而无需将 userId 硬编码到 url 中。任何帮助都会很棒。