How to render a simple JSP page in CQ?

↘锁芯ラ 提交于 2019-12-25 01:34:28

问题


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
  • etc.

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>
<% } %>

How can this be done in CQ?


回答1:


You cannot render a JSP directly in CQ5 as Sling renders only the content/resource and not the scripts.

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"
}

and then call these paths in your AJAX requests.

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

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


来源:https://stackoverflow.com/questions/25300012/how-to-render-a-simple-jsp-page-in-cq

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!