我想知道是否有办法在CQ5中获取页面节点的呈现
HTML输出而不会访问实际的URL.我有Page节点,我想在
java中以编程方式获取该页面节点的呈现HTML输出,并将其存储在一个字符串中而不会访问页面URL.
任何帮助表示赞赏,提前谢谢!
解决方法
节点本身它只是一个数据. Sling框架负责呈现此数据.它使用一堆规则来确定如何渲染这些数据.
Sling Script Resolution Cheet Sheet由于Sling是Web框架,它通过http请求呈现数据.
要在CQ / AEM中模拟此请求,我建议使用com.day.cq.contentsync.handler.util.RequestResponseFactory服务
import org.apache.sling.engine.SlingRequestProcessor; import com.day.cq.contentsync.handler.util.RequestResponseFactory; @Reference private RequestResponseFactory requestResponseFactory; @Reference private SlingRequestProcessor requestProcessor; public String doStuff(){ HttpServletRequest request = requestResponseFactory.createRequest("GET","/path/to/your/node.html"); request.setAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME,WCMMode.DISABLED); ByteArrayOutputStream out = new ByteArrayOutputStream(); HttpServletResponse response = requestResponseFactory.createResponse(out); requestProcessor.processRequest(request,response,resourceResolver); return out.toString(response.getCharacterEncoding()); }
希望能帮助到你.