我如何在@service类中获取HttpServletRequestObject

|| 我已经能够成功集成Spring和Spring4GWT。 一切正常。 我面临的唯一问题是如何在
@service
类中获得
HttpServletRequestObject
? 一些配置和代码 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>
服务等级
public class LoginServiceImpl implements ILoginService {

    private IUserService userService;
    public LoginServiceImpl(IUserService userService) {
        super();
        this.userService = userService;
    }

    public boolean isAuthenticUser(String userName, String password) {
        // operation
    }
}
在ѭ4中,我无法获取Servlet对象。 我在这里需要它,以便可以将其用于其他目的。 任何想法?     
已邀请:
        终于我找到了解决方案。 如果有人希望访问GWT-RPC服务中的HttpServletrequest,则以下内容可以提供帮助。 修改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>
服役中
ServletRequestAttributes sra = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes());
sra.getRequest();
希望这会有所帮助     
        从设计的角度来看,
you should NOT have a HttpServletRequest object in your service layer
。相反,从请求中检索所需的信息,并将它们作为参数传递给服务层。 请进一步说明您要做什么以及为什么在服务层中需要HttpServletRequest对象。 否则,您的依赖关系层次结构将具有循环。视图层取决于服务层,服务层取决于视图层。     
        您需要将HttpServletRequest从控制器传递到Service方法(例如,作为方法参数)。 无论如何: 您的服务方法应从诸如httpServletRequestObject之类的内容中抽象出来。此抽象通常在(Web)控制器中完成。 看起来您是在重新发明安全性内容,您对Spring Security有所了解吗?     
        还有另一种解决方案,当您也需要
HttpServletResponse
时可能会派上用场。 简单的说: 使您的
ServiceImpl
类扩展到
org.spring4gwt.server.SpringGwtRemoteServiceServlet
(无论如何,您都应该这样做) 然后更改
SpringGwtRemoteServiceServlet#getBean()
以返回
this
对象 只要您的
web.xml
是指您的not9ѭ而不是直接使用Spring4GWT,则
this
将是您的
ServiceImpl
扩展
SpringGwtRemoteServiceServlet
的实例。这也是标准的GWT方式,您甚至可以使用@RemoteServiceRelativePath将其自动化。 您还想通过
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this)
运行servlet(上面的
this
对象)来完成依赖注入 这样,您的
ServiceImpl
还将成为响应请求的servlet,并且能够从父类访问getThreadLocalRequest() 它看起来并不那么骇人听闻。基本上,GWT希望您的服务类是servlet,而Spring4GWT希望它是标准的Spring组件。这种方法介于两者之间。     

要回复问题请先登录注册