0

Is there some way to replicate rails' "link-to-unless-current"?

Ie. if i have a list of links in my navigation, and I want to mark the current page with a different css style (or even have it be plain text, not a link)?

Now I just made a method in my action-class (getPage()) which returns a name that I assign for each action, and test for that when building my navigation... Works but not pretty, and I have to manually set the page name (couldn't struts somehow automaticalyy get it from the context).

Using Struts 2, Tiles, JSP + Struts taglibs.

1 Answer 1

4

To get the current url you you can use the Url tag without any params, actions, etc:

<s:url var="currenturl" includeParams="get" escapeAmp="false"/>

Then assuming you construct your navigation Url something like:

<s:url var="url" action="someAction" escapeAmp="false">
    <s:param name="id" value="id"/>
</s:url>

Then you can then use a normal if tag to test whether the current url matches on that you've constructed.

<s:if test="#url eq #currenturl">
</s:if>

For example, to change the class, you can do a conditional inline to your anchor definition:

<a href="<s:property value="#url"/>" <s:if test="#url eq #currenturl">class="current"</s:if>>XXX</a>

Use the includeParams="get" if the parameters in your URL are meaningful for navigation otherwise exclude that and use an if like:

<s:if test="#url.startsWith(#currenturl)">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.