How do I get HttpServletRequestObject in the @service class
I'm successfully able to integrate Spring and Spring4GWT. Everything is working fine.
The only problem I'm facing is How do I get HttpServletRequestObject
in the @service
class?
Some of the configuration and code
web.xml
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/ui/test/*</url-pattern>
</servlet-mapping>
Service class
public class LoginServiceImpl implements ILoginService 开发者_运维知识库{
private IUserService userService;
public LoginServiceImpl(IUserService userService) {
super();
this.userService = userService;
}
public boolean isAuthenticUser(String userName, String password) {
// operation
}
}
In LoginServiceImpl
I'm not able to get the Servlet object.
I need it here so that I can use it for different purposes.
Any Idea?
Finally I got a solution for it. If someone wants to have access of the HttpServletrequest in the GWT-RPC service then the following can help.
Modify 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_pattern/*</url-pattern>
</filter-mapping>
In Service
ServletRequestAttributes sra = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes());
sra.getRequest();
Hope this is helpful
From the design point of view, you should NOT have a HttpServletRequest object in your service layer
. Instead, retrieve whatevet information you need from request and pass them as paramaeters to your service layer.
Please tell more about what you are trying to do and why you need a HttpServletRequest object in your service layer.
Otherwise your dependency hierarchy will have cycle. View layer depending service layer and service layer depending on view layer.
You need to pass the HttpServletRequest from your Controller to the Service Method (for example as method parameter).
Anyway:
- your Service Method should abstract from stuff like httpServletRequestObject. This abstraction is normally done in the (Web) Controller.
- it looks like you reinvent the security stuff, have you had a look at Spring Security?
There is also alternative solution, that might come handy when you also need HttpServletResponse
.
Simply put:
- make your
ServiceImpl
classes to extendorg.spring4gwt.server.SpringGwtRemoteServiceServlet
(you should do it anyway) - then alter the
SpringGwtRemoteServiceServlet#getBean()
to returnthis
object - the
this
will be instance of yourServiceImpl
extendingSpringGwtRemoteServiceServlet
as long as yourweb.xml
is refering to yourServiceImpl
and not Spring4GWT directly. This is also standard GWT way, you can even automate it with @RemoteServiceRelativePath. - you also want to run the servlet (the
this
object from above) throughSpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this)
to get your dependency injection done - this way your
ServiceImpl
will also be the servlet responding to request and will be able to access getThreadLocalRequest() from parent class
It is not that hacky as it looks. Basically GWT wants your service classes to be servlets and Spring4GWT want it to be standard Spring components. This approach is somewhere in between.
精彩评论