问题描述
我们已将AngularJS应用程序部署到CQ5。目前,此应用程序的视图只是静态HTML文件,并存储在下面,例如 / apps / myapp / views /
:
We have deployed an AngularJS app to CQ5. At the moment, the views for this app are just static HTML files and stored underneath, say, /apps/myapp/views/
:
-
/apps/myapp/views/list.html
-
/apps/myapp/views/view.html
- 等。
Angular然后将AJAX请求发送到服务器,并根据需要加载和呈现这些视图。
Angular then sends AJAX requests to the server and loads and renders these views as needed.
我们想向这些页面添加一些动态内容。例如:
We want to add some dynamic content to these pages. For example:
<% if (isGordonFreeman()) { %>
<button>Launch the Hadron Collider!</button>
<% } %>
如何在CQ中完成?
推荐答案
您不能直接在CQ5中呈现JSP,因为Sling仅呈现内容/资源,而不呈现脚本。
You cannot render a JSP directly in CQ5 as Sling renders only the content/resource and not the scripts.
快速修复,您可以创建一个 nt:unstructured
节点,并将 sling:resourceType
属性设置为jsp的路径(
As a quix fix you can create an nt:unstructured
node with the sling:resourceType
property set to the path of the jsp (something like below)
list: {
sling:resourceType: "/apps/myapp/views/list.jsp",
jcr:primaryType: "nt:unstructured"
},
view: {
sling:resourceType: "/apps/myapp/views/view.jsp",
jcr:primaryType: "nt:unstructured"
}
然后调用这些您的AJAX请求中的路径。
and then call these paths in your AJAX requests.
但是,不建议直接指向脚本,因为@BertrandDelacretaz正确地指出了这一点。相反,您可以具有可以满足您的请求的组件。如果要创建的组件太多,则可以借助。
But then, it is not recommended to point directly to scripts, as @BertrandDelacretaz rightly points out. Instead you can have components that can service your requests. If there are too many components to create then you can have a single component service your requests with the help of selectors.
例如,您的组件结构可以如下所示。
For e.g., your component structure can be like the one shown below.
/apps/myapp/views
|_ list.jsp
|_ view.jsp
|_ anythingelse.jsp
和资源(例如 / content / myapp / views
)现在将 sling:resourceType
作为 / apps / myapp / views
。
而您的AJAX请求将是
And the resource( say /content/myapp/views
) will now have the sling:resourceType
as /apps/myapp/views
.And your AJAX requests would be
/content/myapp/views.list.html
/content/myapp/views.view.html
/content/myapp/views.anythingelse.html
这篇关于如何在CQ中呈现一个简单的JSP页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!