Struts2 s:include and passing parameters

Tags:

I am not sure that my understanding of this is quite correct. I seem to have to convert from Struts2 to JSP and back again to make it work. I am currently using:


<s:include value="/WEB-INF/page/admin/include/alphabetic-search-table.jsp">
    <s:param name="searchTerm">/administration</s:param>
</s:include>

In order to include my page with parameters I pass in searchTerm using the <s:param ... > tag. In order to pick up the parameter in the JSP I use:


pageContext.setAttribute("searchTerm" , request.getParameter("searchTerm"));

and then I can grab it using Struts notation:


value="%{#attr.searchTerm}

This seems a little twisted and maybe there is an easier way but I am not sure what it is. The documentation on the struts 2 website for the include tag says this:

Parameters are passed as request parameters, so use the ${param.ParamName} notation to access them. Do not use the property tag to access parameters in included files.

Which sort of suggests I am doing it wrongly but using ${param.ParamName} fails in Struts 2.0.11.2 with the error:

According to the TLD or attribute in tag file, attribute value does not accept any expressions.

Comments:

great post! ... have you find the solution about the correct use of ${param.ParamName} ? ...in some post I read that you should use %{param.ParamName} but it haven't any solution neither joy ....

Best Regards

I did do a little bit of extra reading after writing this post and came to the conclusion that the above is the only way to do this. I didn't see any post that suggested a different way and a couple of posts on the nabble struts forum that did it in the same way.

I have noticed that the s:component tag looks to have similar functionality as the s:include and you may find that suitable but I have never used the s:component tag. The s:component tag also says that you can use:

%{parameters.key1}
%{parameters.get('key1')}

but neither of them worked for the s:include. I also tried:

%{#parameters.key1}
%{#parameters.get('key1')}

but without success.

Thanks for your post.
I find struts2 documentation really poor. I am happy to see that I am not the only one to get problems with things that should be simple.

I think you get your struts error because you are using "${}" notation in the value attribute. "${}" works fine when used in html code.

The following code works (title is my parameter):

Title: ${param.title}

or

<s:set name="myParam">${param.title}</s:set>
Title: <s:property value="%{myParam}"/>

(this second solution just avoid the use of pageContext.setAttribute)

Thanks for the comment Sam, I also find the Struts2 documentation very poor in places; I know, if I find it poor then I should improve it but unfortunately I don't have the spare time at the moment.

I tried the method you mentioned and although the:

Title: ${param.title}

worked perfectly I could not manage to use your second example which I need to do. I found that if I used:

<s:set name="tmpSubmitButtonText">
${param.submitButtonText}
</s:set>

I could not retrieve the value using any of:

<s:property value="#tmpSubmitButtonText"/>
<s:property value="%{#tmpSubmitButtonText}"/>
<s:property value="%{tmpSubmitButtonText}"/>
<s:property value="tmpSubmitButtonText"/>
<s:property value="%tmpSubmitButtonText"/>
<s:property value="%{tmpSubmitButtonText}"/>
<s:property value="%{#tmpSubmitButtonText}"/>

It looked to me as if it might have worked if I had a parameter in my action called 'tmpSubmitButtonText' but I didn't want to clutter the action with redundant code.

I find getting values in my JSP very difficult using Struts. There seems to be no single way to do it. There are horrible combinations of % or # which often seem to fail when using certain struts2 tags but work for others. I have not managed to find the logic of how it works yet.

I think this alone would make me consider using a different MVC framework next time but I will probably find problems in all of them.

Sorry for my very late answer.
Strange that the second code example does not work.
I am using this example without any action and it works:
<s:include value="/jsp/testInclude.jsp">
<s:param name="title" value="%{'Test page title'}" />
</s:include>

with testInclude.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
INCLUDE TEST
-- Title: ${param.title} -&gt OK
-- <s:set name="myParam">${param.title}</s:set>
Title: <s:property value="%{myParam}"/> -&gt OK

Testing the value of a parameter is also a problem. The only thing that seems to work is using the myParam variable:
<s:if test="%{#myParam == 'Test page title'}">TEST OK</s:if>

Tnx, works for me.

Post a Comment:

HTML Syntax: Allowed