简体   繁体   English

使用Java将带有相对URL的锚标记转换为HTML内容中的绝对URL

[英]Converting anchor tag with relative URL to absolute URL in HTML content using Java

The situation: 情况:

On server A we want to display content from server B in line on server A. 在服务器A上,我们要在服务器A上在线显示来自服务器B的内容。

The problem: 问题:

Some of the hyperlinks in the content on server B are relative to server B which makes them invalid when displayed on server A. 服务器B上的内容中的某些超链接是相对于服务器B的,这使得它们在服务器A上显示时无效。

Given a block of HTML code that contains anchor tags like the following 给定一段HTML代码,其中包含如下所示的定位标记

<a href="/something/somwhere.html">Somewhere</a>

what would be the most efficient way to convert them to 将它们转换为最有效的方法是什么

<a href="http://server-b.com/something/somewhere.html">Somewhere</a>

There can be multiple anchor tags in the content, the one catch is that some might be absolute and I want to leave those as they are, I only want to prepend the domain of server B to the relative URLs 内容中可以有多个锚标记,一个要注意的是某些标记可​​能是绝对的,我想保留这些标记,我只想在服务器B的域之前添加相对URL

Depending on a lot of things around how your web app is set up, and also on your definition of efficient, this might not be what you need or are looking for. 根据周围很多你的web应用程序的设置方式,以及您的有效定义的东西,这可能不是你所需要的或所期待的。 But anyway, if you have your HTML as a String (in some late stage of a Filter for example), you could do something like this: 但无论如何,如果你有你的HTML作为一个字符串(在例如过滤器的一些晚期),你可以这样做:

html = html.replaceAll("href=\"/", "href=\"http://server-b.com/")

There is my method, whitch i use for convert relative URLs to absolute. 有一种方法,我用它来将相对URL转换为绝对URL。 I use it for converting some pages to email body. 我用它来将某些页面转换为电子邮件正文。

public String replaceLinks(String address, String content) throws URISyntaxException{
    //absolute URI used for change all relative links
    URI addressUri = new URI(address);
    //finds all link atributes (href, src, etc.)
    Pattern pattern = Pattern.compile("(href|src|action|background)=\"[^\"]*\"", Pattern.CASE_INSENSITIVE);
    Matcher m = pattern.matcher(content);
    //determines if the link is allready absolute
    Pattern absoluteLinkPattern = Pattern.compile("[a-z]+://.+");
    //buffer for result saving
    StringBuffer buffer = new StringBuffer();
    //position from where should next interation take content to append to buffer
    int lastEnd = 0;
    while(m.find()){
        //position of link in quotes
        int startPos = content.indexOf('"',m.start())+1;
        int endPos = m.end()-1;
        String link = content.substring(startPos,endPos);
        Matcher absoluteMatcher = absoluteLinkPattern.matcher(link);
        //is the link relative?
        if(!absoluteMatcher.find())
        {
            //create relative URL
            URI tmpUri = addressUri.resolve(link);
            //append the string between links
            buffer.append(content.substring(lastEnd,startPos-1));
            //append new link
            buffer.append(tmpUri.toString());
            lastEnd =endPos+1;
        }
    }
    //append the end of file
    buffer.append(content.substring(lastEnd));
    return buffer.toString();
}

hope it helps. 希望能帮助到你。

I wouldn't do this in Java; 我不会用Java做到这一点。 I like to handle view-specific logic in the view layer. 我喜欢在视图层中处理特定于视图的逻辑。 I'm assuming this block of code is coming from an AJAX call. 我假设此代码块来自AJAX调用。 So what you can do is get the HTML from the AJAX call and then do this: 因此,您可以做的是从AJAX调用中获取HTML,然后执行以下操作:

jQuery(html).find("a[href]").each(function(index, value) {
  var $a = jQuery(value);
  var href = $a.attr("href");

  if(!/^http:/.test(href)) {
     $a.attr("href", "http://server-b.com" + href);
   }
});

Or if you really want to do this in Java, Lauri's answer will work. 或者,如果您真的想用Java做到这一点,Lauri的答案将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM