我正在实施一个带有apache吊索的网站。某些部分应该是“可编辑的”。
所以我创建了一个自定义Filter,它使用一些标记包装资源,提供编辑选项和模块特定表单以更改属性。到现在为止还挺好。
@SlingFilter(scope = INCLUDE, order = 0)
public class EditContentFilter implements Filter
{
public void init(FilterConfig filterConfig) throws ServletException
{
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
Resource resource = slingRequest.getResource();
if (isComponent(resource))
{
response.getWriter().write(this.buildEditBarHTML(resource));
chain.doFilter(request, response);
response.getWriter().write("</div>");
}
else
{
chain.doFilter(request, response);
}
}
private String buildEditBarHTML(Resource resource)
{
ResourceResolver resourceResolver = resource.getResourceResolver();
Resource componentResource = resourceResolver.resolve("/apps/" + resource.getResourceType());
StringBuilder html = new StringBuilder("<div class='editable' data-resource='" + resource.getPath() + "'>");
if (componentResource != null)
{
Resource componentDialog = componentResource.getChild("form");
if (componentDialog != null)
{
Iterable<Resource> dialogItems = componentDialog.getChildren();
html.append("<div class='edit-pannel'><span class='btn btn-primary edit-component' onclick=\"openDialog('").append(this.buildDialogId(resource.getPath())).append("')\">edit</span><a href='#' class='btn btn-danger'>delete</a></div>");
html.append("<div id=\"").append(this.buildDialogId(resource.getPath())).append("\" class=\"overlay\">");
html.append("<a href=\"javascript:void(0)\" class=\"closebtn\" onclick=\"closeDialog('").append(this.buildDialogId(resource.getPath())).append("')\">×</a>");
html.append("<div class=\"overlay-content\">");
html.append("<form class=\"form-inline\" method=\"post\" action=\"#\">");
for (Resource dialogItem : dialogItems)
{
html.append(buildDialogItem(dialogItem));
}
html.append("<button type=\"submit\" class=\"btn btn-primary\">save</button>");
html.append("</form>");
html.append("</div>");
}
else
{
html.append("<div class='edit-pannel'><span class='btn btn-info disabled edit-component'>edit</span><a href='#' class='btn btn-danger'>delete</a></div>");
}
}
html.append("</div>");
return html.toString();
}
一切都很好。但我真的不喜欢用我的java后端代码手动创建该标记。
所以我认为创建一个生成编辑面板然后包含实际组件的组件是一个好主意。这样的事情。
<div class='editable' data-resource="${resource.path}">
<!-- Edit Pannel -->
<sly data-sly-resource="${resource.path @ resourceType='website/dialog/editPanel'}"></sly>
<!-- Include component -->
<sly data-sly-resource="${resource.path}"></sly>
<!-- Edit Overlay -->
<sly data-sly-resource="${resource.path @ resourceType='website/dialog/editOverlay'}"></sly>
</div>
但我不知道如何覆盖Filter上的资源类型。 你们有没有人想解决这个问题?