Can you use JSTL c: if checking url pattern?

I am trying to set a JSP condition acting on the current URL. Basically, I want to do something if the URL ends with /my-suffix, not including the query string, etc. Therefore, I need to test the substring url, which is located right in the middle.

<c:if test="url not including query string+ ends with '/my-suffix'">
  do something...
</c:if>

Is there any way to do this?

+5
source share
3 answers

Make JSTL taglib functions . One of the available features fn:endsWith(). This allows you, for example, to do:

<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<c:if test="${not fn:endsWith(pageContext.request.requestURI, '/my-suffix')}">
    <p>URL does not end with /my-suffix.</p>
</c:if>

( ${pageContext.request.requestURI}returns HttpServletRequest#getRequestURI()that does not include the query string)

, , Servlet 3.0, , , EL 2.2, Tomcat 7, Glassfish 3 .., , String#endsWith():

<c:if test="${not pageContext.request.requestURI.endsWith('/my-suffix')}">
    <p>URL does not end with /my-suffix.</p>
</c:if>
+12

BalusC , URL:

<c:if test="${fn:contains(pageContext.request.requestURI, 'my-string')}">
    <p>URL Contains my string</p>
</c:if>

Sidenote: , ( ), , OP . , (jstl ). - , , .

+4

URL-, frisbee , end-with .. .

, ?

, →

vars URL , :

<c:set var="url" value="${ pageContext.request.requestURI }" />
<c:set var="stringToHunt" value="Your-String-Here" />

, indexOf ( -1, ​​):

<c:if test="${ fn:indexOf( url , stringToHunt) ne -1 }">
    This page does not contain your string!
</c:if>

** JSTL/EL - nav, . - ! .

0

All Articles