gpt4 book ai didi

spring - Session 空指针异常

转载 作者:行者123 更新时间:2023-12-02 08:44:57 24 4
gpt4 key购买 nike

我正在使用 spring4gwt在我的项目中。

我有以下登录服务实现:

@Service("loginService")
public class LoginServiceImpl extends RemoteServiceServlet implements LoginService {

@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public UserBean checkUser(String userName, String password) throws Exception {

HttpSession httpSession = getThreadLocalRequest().getSession();

}
}

当我调用 loginService.checkUser("test","test")(在托管模式下)时,我得到一个 NullPointerException,因为 getThreadLocalRequest() 返回 NULL而不是实际 session 。

我还没有在网络模式下尝试过。

为什么我会得到一个空 session ?跟spring4gwt有关系吗?

最佳答案

是的,因为 spring4gwt,我们需要一个不同的方法。

我在这里找到了解决方案 http://code.google.com/p/spring4gwt/issues/detail?id=2 :

在 web.xml 中:

<filter>
<filter-name>springRequestFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>springRequestFilter</filter-name>
<url-pattern>/your-url/*</url-pattern>
</filter-mapping>

而不是:

 HttpSession httpSession = getThreadLocalRequest().getSession();

使用:

ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpSession httpSession = attr.getRequest().getSession();

关于spring - Session 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13168648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com