Talha Ahmed Khan
Talha Ahmed Khan

Reputation: 15433

Struts2, Cannot access param using <s:include> tag. Want to use it in <s:if> tag

I have a situation that I need to put my NAV bar at the top of each page. So I decided to Include a new JSP in each page having a parameter indicating the what is the active tab the user is on.

My Implementation is as follows.

dashboard.jsp

...
<s:include value="../tab-set.jsp">
    <s:param name="tab_name" value="dashboard" />
</s:include>
...

tab-set.jsp

<nav>
    <ul>
        <li <s:if test="param.tab_name == 'dashboard'">class="active"</s:if> >
            <a href="dashboard">Dashboard</a>
        </li>
        <li <s:if test="param.tab_name == 'tab_2'">class="active"</s:if> >
            <a href="suggestion">TAB 2</a>
        </li>
    </ul>
</nav>

The result is that IF case do not execute on both tabs.

I have also tried it with different approaches but its not working like

<s:if test="#param.tab_name == 'dashboard'">

OR

<s:if test="#attr.tab_name == 'dashboard'"> (found some place on the net)

OR I also tried to print the value of tab_name value on page using ${param.tab_name} but nothing happened.

But none of them are working.

Please help me or guide me what I can do instead.

Thanks.

Upvotes: 1

Views: 4190

Answers (4)

Pramod Tiwari
Pramod Tiwari

Reputation: 297

Please Change dashboard.jsp File with following code :

<s:include value="../tab-set.jsp">
    <s:param name="tab_name">dashboard</s:param>
</s:include>

If you declared the “String” value inside the “value” attribute, Struts 2 will ignore it.

Upvotes: 0

Atropo
Atropo

Reputation: 12531

This is the documentation from the struts2 on how to use the include tag: documentation

Upvotes: 0

Aleksandr M
Aleksandr M

Reputation: 24396

The parameters cannot be accessed that way because valuestack is not created within rendered page. Try to access them like request parameters ${param.tab_name}.

Update

The value of <s:param> tag should be 'dashboard' because it is a string.

<s:include value="../tab-set.jsp">
    <s:param name="tab_name" value="'dashboard'" />
</s:include>

In your included page get tab_name using ${param.tab_name} notation and set it to some other variable using <s:set> tag.

<s:set name="tabName">
   ${param.tab_name}
</s:set>
<s:if test="#tabName == 'dashboard'">
</s:if>

This way there is no need to use scriplets.

Upvotes: 2

Andrea Ligios
Andrea Ligios

Reputation: 50203

The trick is in the missing piece:

<% pageContext.setAttribute("tab_name" , request.getParameter("tab_name")); %>

Then, do like tihs:

tab-set.jsp

<% pageContext.setAttribute("tab_name" , request.getParameter("tab_name")); %>
<nav>
    <ul>
        <li <s:if test="#attr.tab_name == 'dashboard'">class="active"</s:if> >
            <a href="dashboard">Dashboard</a>
        </li>
        <li <s:if test="#attr.tab_name == 'tab_2'">class="active"</s:if> >
            <a href="suggestion">TAB 2</a>
        </li>
    </ul>
</nav>

Enjoy

Upvotes: 0

Related Questions