Eve
Eve

Reputation: 514

How to remove the last part of the current url using jstl

I was wondering if you can remove the last part of the url using JSTL. My current link is

http://localhost:8080/program/year/eventid/resources

I want to remove the /resources (/resources is dynamic is will change depending on the page I am visiting example /home or /logistics) part and use the url again to go back to the home page what I used to get the current URL is

<c:set var="currenturl" value="${pageContext.request.requestURL}" />

I cannot really find an answer to this question with only the use of JSTL, so no javascript or JAVA. That would be my last option.

So can anybody help me with this?

Upvotes: 1

Views: 2171

Answers (2)

Jasper de Vries
Jasper de Vries

Reputation: 20168

You could, if you really want to:

<c:set var="splitUrl" value="${fn:split(pageContext.request.requestURL, '/')}" />
<c:forEach items="${splitUrl}" var="part" varStatus="status">
  <c:if test="${not status.last}">
    <c:set var="trimmedUrl"
           value="${trimmedUrl}${status.first ? '' : '/'}${part}"/>
  </c:if>
</c:forEach>

But the better way would be creating a custom tag or function to do the work. Benefits:

  • Reusable
  • Better readable JSP

Upvotes: 1

sp00m
sp00m

Reputation: 48807

Maybe you could have a look at the JSTL function fn:replace() (tuto, doc).

Upvotes: 1

Related Questions