jstl分割功能 [英] jstl split function

查看:58
本文介绍了jstl分割功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jstl 1.2拆分给定的字符串 例如:

I wanted to split a given string using jstl 1.2 eg:

Bean thesis.url contains "http:website1.com : http:website2.com"
which needs to be splited into
http:website1.com
http:website2.com

<c:set var="url">
  <c:out value="${thesis.url}" />
</c:set>  

<c:set var="offUrls" value="${fn:split(url,' : ')}" />
<c:forEach items="${offUrls}" var="link">
    <a href=" <c:out value='${link}' />" target="_blank">
        <c:out value="${link}" />
    </a>
</c:forEach>

但是输出不想要我想要的是
http
website1.com
http
website2.com

But the output is not want I wanted which is
http
website1.com
http
website2.com

我尝试了另一种方法,它的工作也可行.
<c:set var="_split" value= " : "/>
<c:set var="offUrls" value="${fn:split(url,_split)}" />

I tried another way, and its dint work either.
<c:set var="_split" value= " : "/>
<c:set var="offUrls" value="${fn:split(url,_split)}" />

推荐答案

fn:split会将您的字符串分割为任何定界符,因此在您的情况下,空格和:都将被分割.解决方法是先做fn:replace:

fn:split will split your string on any of the delimiter characters, so in your case both space and :. The solution is to do a fn:replace first:

<c:set var="urls" value="http://website1.com : http://website2.com"/>
<c:set var="urls" value="${fn:replace(thesis.url, ' : ', '|')}"/>

请确保将分隔符替换为字符串中不存在的字符,否则您将遇到相同的问题.现在您可以使用fn:split(urls, '|'),但是使用 <c:forTokens/> :

Make sure to replace the separator with a character that is not present in your string, or else you will run into the same problem. Now you can use fn:split(urls, '|'), but it would be easier to use <c:forTokens/>:

<c:forTokens items="${urls}" delims="|" var="url">
  <a href="${url}">${url}</a>
</c:forTokens>

更好的解决方案是只在应用程序的后端进行工作,然后将字符串列表传递到前端.

A better solution would be to simply do the work at the back end of your application and pass a list of strings to the front end.

这篇关于jstl分割功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆