将JSP包含到吊索servlet中 [英] Including a JSP into a sling servlet

查看:105
本文介绍了将JSP包含到吊索servlet中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在做一个小项目,试图帮助某人弄清楚如何连接一个组件.

I'm currently working on a small project, trying to help someone figure out how to wire up a component.

理想情况下,我们想做两件事:

Ideally we'd like to do 2 things:

  1. 有一个呈现模板的jsp
  2. 在SlingAllMethodServlet中拥有我们所有的业务登录信息

servlet定义要点:

Gist of servlet definition:

package definition...

import statements...

@SuppressWarnings("serial")
@SlingServlet(
    resourceTypes="path/to/my/component",
    methods="GET",
    extentions="HTML")
@Properties({
  @Property(name="service.pid", value="<my service class>", propertyPrivate=false),
  @Property(name="service.description",value="<description>", propertyPrivate=false),
  @Property(name="service.vendor",value="<company>", propertyPrivate=false)
})

public class MyComponentServlet extends SlingAllMethodsServlet {
  @Override
  protected void doGet  (SlingHttpServletRequest pRequest, SlingHttpServletResponse pResponse) throws ServletException, IOException {
    ...
  }

  @Override
  protected void doPost(SlingHttpServletRequest pRequest, SlingHttpServletResponse pResponse) throws ServletException, IOException {
    ...
  }
}

当我在运行该页面的组件中包含该组件时,这实际上效果很好.问题(正如您可能预期的那样)是,我在这里使用了HTML扩展名.因此,"component.jsp"不会被渲染.

This actually works great, when I include the component on a page this runs. The problem (as you might expect) is that I'm consuming the HTML extension here. So "component.jsp" isn't getting picked up for render.

我很好奇,如果有人知道如何执行以下操作之一:

I'm curious if someone knows how to do one of the following:

  1. 在此servlet中包含用于呈现的JSP(即,我在6dimensions上看到了一些有关pageContext#include和pageContext#pushBody的有趣内容:设置此servlet,以便该servlet在呈现JSP之前在该路径上运行.

    Set up this servlet, so that this servlet runs at that path before the JSP is rendered.

    任何见识都会很棒.

    谢谢你, 布罗迪

    推荐答案

    包含脚本

    使用以下结构将脚本/apps/test.jsp包含在servlet中,并将一些值(绑定)传递给它:

    Use following construction to include script /apps/test.jsp inside the servlet and pass some values (bindings) to it:

    @Reference
    private ServletResolver servletResolver;
    
    public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        Servlet servlet = servletResolver.resolveServlet(request.getResource(), "/apps/test.jsp");
        request.setAttribute("sampleBinding", "bindingValue");
        servlet.service(request, response);
    }
    

    脚本本身可能如下所示:

    The script itself may look like this:

    <%@page session="false" contentType="text/html; charset=utf-8"
    %><%@include file="/libs/foundation/global.jsp"%>
    
    Binding value: ${sampleBinding}
    

    使用模型

    您还可以考虑使用第二种方法-不要为每个组件创建servlet,而是坚持使用JSP,并在每个脚本的开头创建一个 model 对象.示例脚本:

    You may also consider a second approach - don't create servlet for each component, but stick to JSP and at the beginning of each script create a model object. Sample script:

    <%@page session="false" contentType="text/html; charset=utf-8"
    %><%@include file="/libs/foundation/global.jsp"%><%
      pageContext.setAttribute("model", new MyModel(slingRequest, slingResponse));
    %>
    
    Value from model: ${model.value}
    

    和示例模型:

    public class MyModel {
    
        private final SlingHttpServletRequest request;
    
        private final SlingHttpServletResponse response;
    
        public MyModel(SlingHttpServletRequest request, SlingHttpServletResponse response) {
            this.request = request;
            this.response = response;
        }
    
        public String getValue() {
            // you may use request & response objects here
            return "sample value";
        }
    }
    

    如果您喜欢这种方法,则可以使用一个使编写此类模型更加容易的框架.两种有趣的解决方案是:

    If you like this approach, you may use a framework that makes writing such models easier. Two interesting solutions are:

    • Sling models
    • Slice

    这篇关于将JSP包含到吊索servlet中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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