使用 JSTL 将字符串的某个元素更改为可点击的 link
Changing a certain element of a string into a clickable link using JSTL
我正在尝试制作 Twitter 克隆。现在我正在尝试显示用户发布的任何 'hashtags' 作为可点击链接。我正在做的是拆分字符串,如果第一个字符以井号开头,则将其包装在锚标记中。目前我在控制器上有代码,但想知道是否有办法在 JSTL 的视图页面上执行此操作。
Transmit tmit = tService.findTransmit(usern.getId());
String post = tmit.getContent().toString();
String[] split = post.split(" ");
for (int i = 0; i < split.length; i++) {
if (split[i].charAt(0) == '#'){
split[i] = "<a href='/search'>" + split[i] + "</a>";
}
}
String joined = String.join(" ", split);
有没有办法在 JSTL 中做到这一点?目前我正在将所有帖子添加到我的模型中并循环遍历它们:
<c:forEach var="post" items="${posts}">
<c:out value="${post.user.name}"/>
<c:out value="${post.content}" />
</c:forEach>
我的想法在 ${post.content}
上循环,有什么办法可以实现吗?如有任何想法,我们将不胜感激!
没有直接的 JSTL method/tag 可以用锚定的主题标签替换您的主题标签数据。但是您可以定义自己的 JSTL 标记,它可以为您完成这项工作。例如
Create a class which contains a method to replace your hashtag data to
anchored tags
public class HashtagFunctions {
public static String replaceHashTags(String s) {
//Use your existing code of replacing hashtags
}
}
Include this class in a tag library descriptor.
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>myfn</short-name>
<uri>http://www.whatever.com/taglib/trlbt</uri>
<function>
<name>replaceHashTags</name>
<function-class>
qualified.path.of.tld.class.HashtagFunctions
</function-class>
<function-signature>
String replaceHashTags(String s)
</function-signature>
</function>
<!-- more functions -->
</taglib>
Now use this tag in JSP
${myfn:removeTags(${post.content})}
我正在尝试制作 Twitter 克隆。现在我正在尝试显示用户发布的任何 'hashtags' 作为可点击链接。我正在做的是拆分字符串,如果第一个字符以井号开头,则将其包装在锚标记中。目前我在控制器上有代码,但想知道是否有办法在 JSTL 的视图页面上执行此操作。
Transmit tmit = tService.findTransmit(usern.getId());
String post = tmit.getContent().toString();
String[] split = post.split(" ");
for (int i = 0; i < split.length; i++) {
if (split[i].charAt(0) == '#'){
split[i] = "<a href='/search'>" + split[i] + "</a>";
}
}
String joined = String.join(" ", split);
有没有办法在 JSTL 中做到这一点?目前我正在将所有帖子添加到我的模型中并循环遍历它们:
<c:forEach var="post" items="${posts}">
<c:out value="${post.user.name}"/>
<c:out value="${post.content}" />
</c:forEach>
我的想法在 ${post.content}
上循环,有什么办法可以实现吗?如有任何想法,我们将不胜感激!
没有直接的 JSTL method/tag 可以用锚定的主题标签替换您的主题标签数据。但是您可以定义自己的 JSTL 标记,它可以为您完成这项工作。例如
Create a class which contains a method to replace your hashtag data to anchored tags
public class HashtagFunctions {
public static String replaceHashTags(String s) {
//Use your existing code of replacing hashtags
}
}
Include this class in a tag library descriptor.
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>myfn</short-name>
<uri>http://www.whatever.com/taglib/trlbt</uri>
<function>
<name>replaceHashTags</name>
<function-class>
qualified.path.of.tld.class.HashtagFunctions
</function-class>
<function-signature>
String replaceHashTags(String s)
</function-signature>
</function>
<!-- more functions -->
</taglib>
Now use this tag in JSP
${myfn:removeTags(${post.content})}