Session的空指针异常

Maj*_*ssi 2 session gwt spring spring-4-gwt

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();

    }
}
Run Code Online (Sandbox Code Playgroud)

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

我还没有尝试过网络模式.

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

Maj*_*ssi 6

是的,因为spring4gwt,我们需要一个不同的approch.

我在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>
Run Code Online (Sandbox Code Playgroud)

而不是:

 HttpSession httpSession = getThreadLocalRequest().getSession();
Run Code Online (Sandbox Code Playgroud)

使用 :

ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpSession httpSession = attr.getRequest().getSession();
Run Code Online (Sandbox Code Playgroud)