3

I am new to java. I tried to search around but did not find anything similar to my question.

I have list items in menu (<ul>-s and <li>-s) I want to add the active class in <li> tag depending on the action's name to make the specific menu item active.

For this I have used this piece of code

${request.context['struts.actionMapping'].name}

This piece of code is working fine on all pages except on page were action name is "" i.e empty string. The empty string check or null check is not working where action name is just ""

<s:set var="action">${request.context['struts.actionMapping'].name}</s:set>

<ul class="nav navbar-nav navbar-right">
    <li class="<s:if test="%{#action == ''}">active</s:if>">
        <s:a href="/crudoperation">Dashboard</s:a>
    </li>
    <li class="<s:if test="%{#action == 'add'}">active</s:if>">
        <s:url var="addUrl" action="add"/>
        <s:a href="%{addUrl}">Add Student Profile</s:a>
    </li>
    <li class="<s:if test="%{#action == 'edit'}">active</s:if>">
        <s:url var="editUrl" action="edit"/>
        <s:a href="%{editUrl}">Edit Profile</s:a>
    </li>
    <li class="<s:if test="%{#action == 'view'}">active</s:if>">
        <s:url var="viewUrl" action="view"/>
        <s:a href="%{viewUrl}">View Profile</s:a>
    </li>
</ul>

Here is my struts.xml

<struts>
    <constant name="struts.ui.theme" value="simple" />
    <package name="default" extends="struts-default" namespace="/">

        <result-types>
            <result-type name="tiles"
                class="org.apache.struts2.views.tiles.TilesResult" />
        </result-types>

        <action name="" class="com.javaguy.struts2.IndexAction">
            <result name="master" type="tiles">master</result>
        </action>

        <action name="add" class="com.javaguy.struts2.AddAction">
            <result name="add" type="tiles">add</result>
        </action>

        <action name="edit" class="com.javaguy.struts2.EditAction">
            <result name="edit" type="tiles">edit</result>
        </action>

        <action name="view" class="com.javaguy.struts2.ViewAction">
            <result name="view" type="tiles">view</result>
        </action>
    </package>
</struts>
Shog9
  • 156,901
  • 35
  • 231
  • 235
Yasir Wali
  • 39
  • 10
  • Empty action? Why? Use ``. – Aleksandr M Dec 09 '15 at 09:46
  • Maybe he wants to catch only the empty action and not every non-matching URL. There is also the `"*"` trick, and BTW the best would be to use a non-empty name, but I guess the whole point is a bit broad – Andrea Ligios Dec 09 '15 at 09:56
  • How one can call empty action at all? Something like `/.action`. – Aleksandr M Dec 09 '15 at 10:04
  • The correct syntax to get the action in the JSP is now `#request['struts.actionMapping'].name` (not `request.context...`) - see https://stackoverflow.com/a/46111170/1005607 – gene b. Sep 01 '22 at 17:38

1 Answers1

2

In case of empty action name the ${request.context['struts.actionMapping'].name} will produce emptiness in <s:set> tag body which is equal to <s:set var="some"></s:set> and that will assign top object from the value stack to some variable. Usually top object in the value stack is action class instance. So you get something like com.javaguy.struts2.IndexAction@4349f7db in your variable instead of empty string.

To get an empty action name just use OGNL in <s:set> tag.

<s:set var="some" value="#request.context['struts.actionMapping'].name" />

Also like Andrea already mentioned avoid using keywords (such as action) in your variables.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • 1
    A *little* side effect of mixing EL with Struts tags :D – Andrea Ligios Dec 09 '15 at 20:31
  • 1
    @YasirWali: Do you have any further questions regarding this problem? If not, don't forget to accept/upvote answers that helped you. – Aleksandr M Dec 21 '15 at 13:12
  • The correct syntax to get the action in the JSP is now `#request['struts.actionMapping'].name` (not `request.context...`) - see stackoverflow.com/a/46111170/1005607 – gene b. Sep 01 '22 at 17:38