【问题标题】:null pointer exception from Session来自 Session 的空指针异常
【发布时间】:2015-09-03 18:22:00
【问题描述】:

我在我的项目中使用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 而不是实际会话。

我还没有尝试网络模式。

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

【问题讨论】:

  • @Filburt : 标题中的 GWT 关键字不是相关的吗?
  • 在标签里,所以标题里根本没必要。
  • 关键字Session怎么样?它也在标签中..无论如何..
  • 标题中并不是禁止关键字。如果它们被用在一个提出简洁问题的句子中,那么在标题中使用标签是绝对可以的。顺便说一句:查看 Google 搜索结果:SO 标题会自动以最流行的标签作为前缀。在这里的问题列表中,标签前缀只是噪音,应该避免。
  • 那么在这种情况下,SO 标题的前缀是 Spring 标签,与我的问题相比,GWT 标签的相关性较低。答案与 spring4gwt 相关,然后是 spring 本身。

标签: spring session gwt spring-4-gwt


【解决方案1】:

是的,因为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();

【讨论】:

  • 感谢大家发布您的解决方案 :) 这对我帮助很大!
猜你喜欢
  • 1970-01-01
  • 2016-11-07
  • 2015-03-23
  • 1970-01-01
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多