2

我有一个没有 Spring 的 jsf 2.0 应用程序,并且我已经实现了一个缓存作为应用程序范围,应该从 Rest 服务访问它。现在我想调用应该检查缓存的其余 web 服务,但是当我想访问它时,它始终为空。

我已经尝试从 Web 服务和这个https://www.mkyong.com/jsf2/how-to-get-servletcontext-in-jsf-2/访问 FacesContext,但它对我不起作用。

@ManagedBean(eager=true)
@ApplicationScoped
public class CacheController implements Serializable{

    private static final long serialVersionUID = 123L;

    private Map<String, Cache> map = new HashMap<String, Cache>();

    public Map<String, Cache> getMap() {
        return map;
    }

    public void setMap(Map<String, Cache> map) {
        this.map = map;
    }

}


@Path("/service")
public class RestService {

    @POST
    @Path("anlieferung/kennzahlen")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getValueFromCache(String item) throws JSONException, ParseException {

                //is always null
        CacheController cacheController= (CacheController) getServletContext().getAttribute("cacheController");

                //is always null
        FacesContext context = FacesContext.getCurrentInstance();
            Application application = context.getApplication();
            CacheController cacheBean = application.evaluateExpressionGet(context, "#{cacheController}", CacheController.class);

        //doSomeStuff and check if the item is in the Cache (CacheController.getMap())
        }
}

我之前已经通过 jsf 应用程序初始化了缓存并且它可以工作。现在我希望通过 FacesContent 或 ServletContext 获得缓存对象,但它始终为空。我需要创建类似 ServletListener 的东西吗?有人可以给我一个例子吗?谢谢

4

0 回答 0