我正在开发一个Web服务,该服务将由我编写的客户端调用.在Web服务中,我需要使用应用程序范围的对象,这可以减轻系统上应用程序的负担.我已经实现了我的应用程序范围内的对象,如此问题.
I'm developing a Web Service which will be called by clients which are written by me. In the web service I need to use application-wide objects which eases the load of application on the system. I have implemented my application-wide objects as shown in this question.
我可以在jsf页面中毫无问题地使用我的对象,如下所示.
I can use my object in a jsf page with no problem as follows.
MyObject mo = (MyObject) FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get("MyObjectsName");但是在Web服务请求中使用它时,FacesContext.getCurrentInstance()返回null.有什么方法可以在Web服务中使用FacesContext.
But when it comes to use it in a Web Service Request FacesContext.getCurrentInstance() returns null. Is there any way to use the FacesContext in a web service.
推荐答案它为null,因为Web服务不是通过Faces Servlet来的.这就是应有的方式,因为Web服务与JSF无关.
It is null, because web services don't come through the Faces Servlet. And this is the way it should be, because web services have nothing to do with JSF.
如果您使用的是jax-ws实现,则可以使用:
In case you are using a jax-ws implementation, you can use:
@Resource WebServiceContext context;这将注入WebServiceContext,通过它您可以:
This will inject the WebServiceContext, by which you can:
ServletContext servletContext = (ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);有了ServletContext,您就可以访问应用程序作用域的对象.
And having the ServletContext, you can access your application-scope objects.
更多推荐
从Web服务访问FacesContext
发布评论