SlingModels:从Resource改编后是否可以注入SlingHttpServletRequest? [英] SlingModels: Can I inject the SlingHttpServletRequest when adapting from Resource?

查看:133
本文介绍了SlingModels:从Resource改编后是否可以注入SlingHttpServletRequest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是SlingModels的新手,并且注释还不太清楚。我目前正在尝试将一些基本基础组件从AEM 6.2转换为使用SlingModels。

I am new to SlingModels and the annotations aren't very clear yet. I am currently trying to transform some basic foundation components from AEM 6.2 to using SlingModels instead.

对于图像组件,基础JSP使用SlingHttpServletRequest设置ImageDoctype。因此,我尝试了以下操作:

For the image component the foundation JSP uses the SlingHttpServletRequest to set the ImageDoctype. So I tried the following:

@Model(adaptables = {Resource.class})
public class ImageModel {
    @SlingObject
    private SlingHttpServletRequest request;

    @SlingObject
    private Resource resource;
}

但与此同时,请求为空。所以我改用:

But with this the request is null. So I switched to using:

@Model(adaptables = {SlingHttpServletRequest.class})

现在为 reuqest 资源

JSP代码:

<sling:adaptTo adaptable="${slingRequest}" adaptTo="models.ImageModel" var="m"/>

这样做是正确的方法还是有办法适应 resource 仍然可以注入 request

Is this the right way to do it or is there a way to adapt from the resource ans still be able to inject the request?

推荐答案

您不能这样做,因为资源不支持SlingHttpServletRequest。如果您的模型中需要Request,请使其适应Request。

You can not do that, because resource is not SlingHttpServletRequest aware. If you need Request in your model make it adaptable from Request.

有一些库可以做到这一点。它使用筛选器将当前请求存储在ThreadLocal中,然后从中读取它,但我不建议使用此方法。 ThreadLocal只是另一个全局变量。

There was some library which allowed to do that. It used Filter to store current Request in ThreadLocal and then read it from it, but I would not recommend this approach. ThreadLocal is just another global.

这篇关于SlingModels:从Resource改编后是否可以注入SlingHttpServletRequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆