jsp if变量以

时间:2013-12-10 22:55:01

标签: java jsp java-server

我需要查看变量是否以键“ef”开头,如ef1,efabc,efanythink ...如果是,则显示错误消息,我在这里混合使用php和jsp,当然不正确且有错误,我没有了解jsp:

<c:if test="${empty channel.getChannelName()}">
<%
if (string_starts_with(${channelName}, 'ef')) { header("location:http://google.com"); }

或显示此错误div

<div class="error"> This Channel url Portected!</div>

原始档案:http://pastebin.com/ach8PXY9

3 个答案:

答案 0 :(得分:3)

<c:if test='${fn:startsWith(channel.channelName, "ef")}'>

 ...

</c:if>

<c:if test='${not fn:startsWith(channel.channelName, "ef")}'>

 ...

</c:if>

或者你可以使用

'<c:choose> <c:when>'

答案 1 :(得分:2)

Theres是一种JSTL方法!

<c:choose>
    <c:when test="${fn:startsWith(channel.getChannelName(), "ef")}">
        <script type="text/javascript">window.location.replace("http://google.com/");</script></c:when>
    <c:otherwise>
        <div class="error"> This Channel url Portected!</div>
    </c:otherwise>
</c:choose>

http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/fn/tld-summary.html

答案 2 :(得分:0)

也可能:

// Order by numerical order: (Assertion: all things are numeric strings only) 
foreach (var thing in things.OrderBy(int.Parse))
{
    Console.Writeline(thing);
}