The following won't work in a test, but it may work in your running application. Spring's RequestContextListener registers the request in a ThreadLocal:

  private HttpServletRequest getRequest() {
return (HttpServletRequest) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  }

HTH,

Matt

On Jan 22, 2008, at 1:12 PM, Ding, Qin wrote:

Again, my goal is to find out how I can get application URL from my bean in service layer. According to Mike, I made my bean servletContextAware so that I can get application URL by calling Url url = servletContext.getResource(“/index.jsp”); However, I don’t get the host, port information from url.getHost(), urlGetPort ()….
Is there a way I get inject HttpServletRequest to my service bean?
In the BasePage, we get can get HttpServletRequest by calling
(HttpServletRequest) getFacesContext().getExternalContext ().getRequest();


public static  HttpServletRequest getRequest() {
        return (HttpServletRequest) FacesContext.getCurrentInstance()
                                                .getExternalContext()
                                                .getRequest();
}
If I put the above method into my own bean, or uses FacesUtils.getRequest(), I got nullPointerException.

Why the “getRequest()” method in BasePage works but not in my service bean? Seems to me if I couldinject the facesContext to my bean, then I can get HttpServletRequest. With Request object, I should be able to get URL by calling request.getUri or Url.

Please advice. Thank you.

QD
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


Reply via email to