2

I'm using spring4gwt in my project.

I have the following login service implementation:

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

    }
}

When I call the loginService.checkUser("test","test") (In hosted mode), I get a NullPointerException, as getThreadLocalRequest() returns NULL instead of the actual session.

I didn't try in web mode yet.

Why would I get a null session? Does it have something to do with spring4gwt?

kenju
  • 5,866
  • 1
  • 41
  • 41
Majid Laissi
  • 19,188
  • 19
  • 68
  • 105
  • @Filburt : isn't GWT keyword relevent in the title ? – Majid Laissi Oct 31 '12 at 23:08
  • It's in the tags, so it's simply not necessary in the title. – Filburt Oct 31 '12 at 23:09
  • what about the keyword `Session`? it's also in the tags.. Anyways.. – Majid Laissi Oct 31 '12 at 23:11
  • It's not that keywords are *forbidden* in the title. If they are used in a sentence that makes a concise question it's absolutly okay to use tags in the title. Btw: Check out Google search results: SO titles are prefixed automagically with the most popular tag. On the questions list here, tag prefixes are just noise and should be avoided. – Filburt Nov 07 '12 at 07:57
  • well in this case SO title is prefixed with the `Spring` tag which is less relevant than `GWT` tag for my question. The answer was more related to the spring4gwt then spring itself.. – Majid Laissi Nov 07 '12 at 18:49
  • Meta reasoning on the matter: [Should questions include “tags” in their titles?](http://meta.stackexchange.com/q/19190) ... and added spring4gwt tag. – Filburt Nov 08 '12 at 19:01
  • @Filburt thank you. shouldn't the tag be spring4gwt without dashes ? – Majid Laissi Nov 08 '12 at 19:11

1 Answers1

6

Yes it is because of spring4gwt, we need a different approch.

I found the solution here http://code.google.com/p/spring4gwt/issues/detail?id=2:

in 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>

and instead of :

 HttpSession httpSession = getThreadLocalRequest().getSession();

Use :

ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpSession httpSession = attr.getRequest().getSession();
Majid Laissi
  • 19,188
  • 19
  • 68
  • 105