0

我想获取当前登录用户的 id 并使用它来访问他们的特定用户页面。

为了实现这一点,我已经能够使用以下方法获取当前登录的用户名:

@RequestMapping(value = "/main/home", method = RequestMethod.GET)
public String getHomePage(Model model,  Principal principal, HttpServletRequest 

    request) {

    try {

        CustomUser customUser = 

            (CustomUser)SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();

        request.getSession().setAttribute("name",
                customUser.getName());

        request.getSession().setAttribute("userId",
                customUser.getUserId());


        request.getSession().setAttribute("user", customUser);

    } catch (Exception e) {
        e.printStackTrace();
    }
    return "/main/home";
}

然后在 JSP 页面中,我使用以下代码尝试根据用户的 id 获取用户的页面:

     <c:url value="/main/user/?userId=${user.userId}" var="url"/><a href="<c:out 

    value='${url}'/>">User Page</a>

但是这个实现不起作用。当我单击用户页面时,我收到以下错误:

java.lang.IllegalArgumentException: id to load is required for loading
org.hibernate.event.spi.LoadEvent.<init>(LoadEvent.java:87)
org.hibernate.event.spi.LoadEvent.<init>(LoadEvent.java:59)
org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2421)
org.hibernate.internal.SessionImpl.get(SessionImpl.java:974)
com.project.myapp.dao.UserDAO.getUserByID(UserDAO.java:91)
com.project.myapp.dao.UserDAO$$FastClassByCGLIB$$b394c410.invoke(<generated>)
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
com.project.myapp.dao.UserDAO$$EnhancerByCGLIB$$232ac7e1.getUserByID(<generated>)
com.project.myapp.service.UserServiceImpl.getUserByID(UserServiceImpl.java:54)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
$Proxy314.getUserByID(Unknown Source)
com.project.myapp.controller.UserController.getUserPage(UserController.java:70)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
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)

我想知道为什么我不能请求用户的 id 但我可以得到用户的名字。

4

0 回答 0