cq5通过Url

时间:2016-01-29 04:05:17

标签: cq5 aem sling

我正在尝试使用http://localhost:4502/sling/test-services/planet.html

访问以下sling servlet

但是,它给出了404错误,不知道我在这里做错了什么。

@Component
@Service(value=javax.servlet.Servlet.class)
@Properties({
    @Property(name="service.description", value="HTML renderer for Planet resources"),
    @Property(name="service.vendor", value="The Apache Software Foundation"),
    @Property(name="sling.servlet.resourceTypes", value="sling/test-services/planet"),
    @Property(name="sling.servlet.extensions", value="html"),
    @Property(name="sling.servlet.methods", value="GET")
})
@SuppressWarnings("serial")
public class PlanetResourceRenderingServlet extends SlingSafeMethodsServlet {

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");

        final ValueMap properties = request.getResource().adaptTo(ValueMap.class);

        // TODO should escape output - good enough for our tests 
        final PrintWriter pw = response.getWriter();
        pw.println(String.format("<html><head><title>Planet at %s</title></head><body>", request.getResource().getPath()));
        pw.println(String.format("<p>Name: %s</p>", properties.get("name")));
        pw.println(String.format("<p>Distance: %s</p>", properties.get("distance")));
        pw.println("</body></html>");
        pw.flush();
    }

}

是否有可能,我可以在没有&#34; .html&#34;的情况下访问servlet服务。扩展名,如果我删除扩展属性?

我感谢任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:5)

如果要通过URL访问servlet,则需要设置sling.servlet.paths而不是sling.servlet.resourceTypes。类似的问题是answered here.

如果您要设置sling.servlet.resourceTypes属性,则需要访问sling:resourceTypesling/test-services/planet的资源。

您的注释应该是

@Component
@Service(value=javax.servlet.Servlet.class)
@Properties({
    @Property(name="service.description", value="HTML renderer for Planet resources"),
    @Property(name="service.vendor", value="The Apache Software Foundation"),
    @Property(name="sling.servlet.paths", value="/sling/test-services/planet"),
    @Property(name="sling.servlet.extensions", value="html"),
    @Property(name="sling.servlet.methods", value="GET")
})

或者可以使用@SlingServlet注释进一步简化,如下所示

@SlingServlet(paths="/sling/test-services/planet", methods="GET", extensions="html")

确保在OSGi控制台中的Apache Sling Servlet/Script Resolver and Error Handler配置中允许使用以下路径。