使用Java将HTML内容中具有相对URL的锚标记转换为绝对URL
情况: 在服务器A上,我们希望在服务器A上的行中显示来自服务器B的内容 问题是: 服务器B上内容中的某些超链接是相对于服务器B的,这使得它们在服务器A上显示时无效 给定包含锚定标记的HTML代码块,如下所示使用Java将HTML内容中具有相对URL的锚标记转换为绝对URL,java,regex,html-parsing,Java,Regex,Html Parsing,情况: 在服务器A上,我们希望在服务器A上的行中显示来自服务器B的内容 问题是: 服务器B上内容中的某些超链接是相对于服务器B的,这使得它们在服务器A上显示时无效 给定包含锚定标记的HTML代码块,如下所示 将它们转换为最有效的方式是什么 内容中可能有多个锚定标记,一个问题是有些锚定标记可能是绝对的,我想保持它们的原样,我只想将服务器B的域预先添加到相对URL中,我不会在Java中这样做;我喜欢在视图层中处理特定于视图的逻辑。我假设这段代码来自一个AJAX调用。因此,您可以从AJAX调用中获
将它们转换为最有效的方式是什么
内容中可能有多个锚定标记,一个问题是有些锚定标记可能是绝对的,我想保持它们的原样,我只想将服务器B的域预先添加到相对URL中,我不会在Java中这样做;我喜欢在视图层中处理特定于视图的逻辑。我假设这段代码来自一个AJAX调用。因此,您可以从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);
}
});
或者,如果你真的想用Java来做这件事,劳里的答案会有用。我不会用Java来做这件事;我喜欢在视图层中处理特定于视图的逻辑。我假设这段代码来自一个AJAX调用。因此,您可以从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);
}
});
或者,如果你真的想用Java来实现这一点,Lauri的答案会起作用。这取决于你的web应用程序的设置方式,也取决于你对高效的定义,这可能不是你需要或想要的。但无论如何,如果您将HTML作为字符串(例如在过滤器的某个后期阶段),您可以执行以下操作:
html = html.replaceAll("href=\"/", "href=\"http://server-b.com/")
这可能不是您所需要或正在寻找的,这取决于web应用程序的设置方式以及您对高效的定义。但无论如何,如果您将HTML作为字符串(例如在过滤器的某个后期阶段),您可以执行以下操作:
html = html.replaceAll("href=\"/", "href=\"http://server-b.com/")
这是我的方法,我使用它将相对URL转换为绝对URL。我使用它将一些页面转换为电子邮件正文
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();
}
希望有帮助。有我的方法,我用它将相对URL转换为绝对URL。我使用它将一些页面转换为电子邮件正文
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();
}
希望有帮助。Java?那么您使用的是JSP/Servlet?您想在运行时(动态)还是仅在开发时(在所有文件中静态查找和替换)?运行时,JSP页面对本地servlet进行ajax调用,该servlet使用API调用(有点像代理)从服务器b中提取内容。我们最初认为让servlet以“工作顺序”返回html片段是最好/最容易的,但是在阅读Vivin的响应之后,让视图解释来自servletJava的响应可能是最好的?那么您使用的是JSP/Servlet?您想在运行时(动态)还是仅在开发时(在所有文件中静态查找和替换)?运行时,JSP页面对本地servlet进行ajax调用,该servlet使用API调用(有点像代理)从服务器b中提取内容。我们最初认为让servlet以“工作顺序”返回html片段是最好的/最简单的方法,但是在阅读Vivin的响应之后,最好让视图解释来自ServletThank的响应,尽管这是一个Javascript解决方案,但对我来说效果很好。谢谢,虽然这是一个Javascript解决方案,但对我来说效果很好。