7

当点击 cq5 页面时,我试图在链接中保留特定的选择器。

例如,假设您已访问 /content/mysite/mypage.stickyselector.html,我希望页面上的所有后续链接,例如 aboutus.html 和 contact.html 页面保留 aboutus.stickyselector.html 和contact.stickyselector.html 链接。

我尝试这样做有几个原因,包括防止在移动设备命中时过度重写,例如 mypage.smart.html,因为我们可以让重写规则允许用户在不重新检测设备类型的情况下通过,如以及任何定制的内容等。

我尝试创建自己的链接重写转换器,这对于重写您手头所有信息的链接非常有用,但是,我似乎无法获得用于访问包含链接的页面的选择器这点。

任何帮助将不胜感激。

4

3 回答 3

5

如果您有权访问吊索请求,则可以通过以下方式获取所有选择器的字符串

String selectors = slingRequest.getRequestPathInfo().getSelectorString();
于 2013-08-15T13:04:29.557 回答
5

There are a couple of approaches:

  1. Rewrite the urls on the outbound response in CQ (as suggested by David)
  2. Rewrite the urls on the inbound request using mod_rewrite at dispatcher (assuming Apache)

As David covered scenario 1, I'll describe the second scenario

The "sticky" selector value can be returned in a cookie to the client

Set-Cookie: selector=stickyselector;

Each subsequent request to the site from the client will contain that cookie. You can then use that cookie to rewrite in the URL in apache before it gets presented to the dispatcher module (and eventually the publish instance:

RewriteCond %{HTTP:Cookie} selector=([^;]+) [NC]    # If we have the selector cookie
RewriteRule ^(.*)\.html$ /$1.%1.html [PT]           # add it to the request url before extension

So a request that arrives at the dispatcher looking like this:

GET /content/mysite/mypage.html HTTP/1.1
Cookie: selector=stickyselector;

Would arrive at the publish instance rewritten as:

/content/mysite/mypage.stickyselector.html

If you are using this approach for device/channel specific renditions then you could alternatively use the user agent value instead of a cookie to drive selector addition. For example:

RewriteCond %{HTTP_USER_AGENT} "iphone|ipod|iemobile" [NC]
RewriteRule ^(.*)\.html$ /$1.mobile.html [PT]              # add channel selector to the request url 

Positives of this approach are that all users are presented with the same URL (e.g./content/mysite/mypage.html) the selectors in URLs are only presented to CQ.

Negatives are that it normally would require cookies and it depends on apache configuration.

于 2013-08-15T23:34:38.327 回答
1

听起来您正在使用重写管道。您是否考虑过以下问题:

public class YourTransformer implements Transformer {
    protected SlingHttpServletRequest request;
    String selectors = "";

    public void init(ProcessingContext context, ProcessingComponentConfiguration config) throws java.io.IOException { {
        request = context.getRequest();
        String[] _selectors = request.getRequestPathInfo().getSelectors();
        StringBuilder buff = new StringBuilder();
        for (String _selector : _selectors) {
            buff.append(".").append(_selector);
        }
        selectors = buff.toString();
    }

    // rest of the methods go here
}

现在,当您重写链接时,您的选择器字符串就可以使用了。

于 2013-08-20T04:15:01.423 回答