How do I get HttpServletRequestObject in the @service class

浪尽此生 提交于 2019-12-07 13:38:24

问题


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?


回答1:


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




回答2:


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.




回答3:


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?



回答4:


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 return this object
  • the this will be instance of your ServiceImpl extending SpringGwtRemoteServiceServlet as long as your web.xml is refering to your ServiceImpl 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) through SpringBeanAutowiringSupport.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.



来源:https://stackoverflow.com/questions/6353656/how-do-i-get-httpservletrequestobject-in-the-service-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!