Struts2, Unable to access the parameter using the <s: include> tag. Want to use it in the <s: if> tag

I have a situation where I need to place my NAV bar at the top of each page. Therefore, I decided to include a new JSP on each page with a parameter indicating which 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> 

As a result, the IF case is not executed on both tabs.

I also tried it with different approaches, but it does not work as

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

OR

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

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

But none of them work.

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

Thanks.

+1
source share
4 answers

It is not possible to access these parameters because valuestack is not created on the displayed page. Try to access them as query parameters ${param.tab_name} .

Update

The value of the <s:param> must 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, enter tab_name using the notation ${param.tab_name} and set it for some other variable using the <s:set> .

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

Thus, there is no need to use scripts.

+2
source

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

0
source

Focus on the missing fragment:

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

Then, 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

0
source

Modify the dashboard.jsp file with the following code:

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

If you specified a String value inside the value attribute, Struts 2 will ignore it.

0
source

All Articles