I wanted to pass more than one parameter for an action. but when i build the URL using Struts2, it builds with only one parameter. May i know what is went wrong in the below code?
<s:url action="loadValidLevelValueDropDown" id="levelvalueURL" escapeAmp="false">
<s:param name="hierarchyId" value="searchAttribute.hierarchyId.id"></s:param>
<s:param name="valuebycoulmn" value="refcolumnName%{#level.count}"></s:param>
</s:url>
result is,
/appname/loadValidLevelValueDropDown.action?hierarchyId=1
The value attribute of <s:param> tag takes OGNL expression as a value. Your refcolumnName%{#level.count} value is not a valid expression so parameter is not being appended to url.
If your refcolumnName is a collection than you can access it like that:
<s:param name="valuebycoulmn" value="refcolumnName[#level.count]" />
if it supposed to be a string than you need to append it like string:
<s:param name="valuebycoulmn" value="'refcolumnName' + #level.count" />
Related
I'm am still pretty new to struts and am having trouble trying to compare two struts params in a struts if statement. I am trying find if the current year param is equal to the checkYear param
If they are equal I want to execute some code. If they are not equal then I would like to execute some other code..
Sample code:
<s:param name="currentYear">
<s:date name="%{new java.util.Date()}" format="yyyy" />
</s:param>
<s:param name="checkYear">
<s:date format="yyyy" name="#year"/>
</s:param>
<s:if test="${checkYear == legendYear}">
code executed
</s:if>
I don't really understand the purpose of the '$','%', or '{}' in the test part of the if statement if or how I need to apply it to have it check the params.
Thank you in advance!
Inside Struts2 tags, you use OGNL. Refer to the small language guide to see how it works.
In OGNL, %{} means you want to force the evaluation of an expression; it is needed somewhere, while it is superfluos somewhere else. In the <s:if/>, it is not needed, because test="" will evaluate its content by default.
${} does not exists in OGNL, it is JSP EL. Search on StackOverflow for some nice answer on that if you are curious.
{} alone in OGNL is List Projection... you probably don't need it now.
In Struts2 tutorial for s:select tag I've seen:
<s:select label="Pets"
name="petIds"
list="petDao.pets"
listKey="id"
listValue="name"
multiple="true"
size="3"
required="true"
value="%{petDao.pets.{id}}"
/> ^ ^
and my question: why value="%{petDao.pets.{id}}"? why not simply value="%{petDao.pets.id}"? what do those trailing curly braces mean?
This is an OGNL list projection to get all the id values as a list from petDao.pets, meaning all values in this <s:select> will be pre-selected.
It isn't necessary; I suspect it was the result of an error in the tag's source file.
It works with it, but isn't needed, will fail IDE validation (if the IDE supports S2 and/or OGNL, e.g., IntelliJ), and I've made a note to update.
The principal reason is because %{} syntax is used to force OGNL evaluation where Struts would otherwise be treating the value as a String literal.
For example,
<s:property value="name" />
will look for a name property in the value stack i.e. is a value retrieved by calling getName().
If you wanted to force it to use literal value "name", you will need to use the %{} syntax -
<s:property value="%{'name'}" />
Source:
http://www.coderanch.com/t/420711/Struts/Struts
I am displaying the list values with tag.At the same time i want to provide a hyperlink for the displaying values to provide action for the displaying value.I am using <s:param> for that.but the values are not passing.I am writing like below
<s:iterator status="stat" value="transactionList">
<s:url id="open" action="openTransaction">
<s:param name="transactionCode" value="<s:property value='monthName'/>"/>
</s:url>
<tr class="gradeB">
<td>
<s:a href="%{open}"><s:property value='transactionCode'/></s:a>
</td>
<td><s:property value="monthName"/></td>
<td><s:property value="transactionDesc"/></td>
</tr>
</s:iterator>
Now the transactionCode property is displaying with hyperlinks and by clicking on that the action is forwarding to openTransaction method.but the value i passed with <s:param> is not going,it is giving null. In iteration for that particular transaction code i want to pass that particular transaction code only. In struts 1.x I used display tag table decorator for this purpose, it takes the current row object.Here also i want to pass the current row values to the action. Please help me.
If you want to use <s:property> tag to put a param inside an url you have to do it like that:
<s:url ...>
<s:param name="foo"><s:property value="bar"/></s:param>
</s:url>
The documentation of <s:param> explains the difference between using this way of putting a param and your way.
Note: When you declare the param tag, the value can be defined in either a value attribute or as text between the start and end tag. Struts behaves a bit different according to these two situations. This is best illustrated using an example:
<param name="color">blue</param> <-- (A) -->
<param name="color" value="blue"/> <-- (B) -->
In the first situation (A) the value would be evaluated to the stack as a java.lang.String object. And in situation (B) the value would be evaluated to the stack as a java.lang.Object object.
For more information see WW-808.
Edit: Also remember that if you are using 2.1.x or higher, the id attribute of <s:url> is deprecated and it has been replaced with var. You can read it here.
<c:url var="search" value="/image/search.action">
<c:forEach items="${filtersMap}" var="map">
<c:param name="filtersMap.${map.key}" value="${map.value}"/>
</c:forEach>
${filtersMap} is a map param from action
i just found something funny, i cannot explain it. can someone explain it.
I have define a url in my jsp file
<s:url var="test" action="Home.action" escapeAmp="false">
<s:param name="departYear" value="2006"/>
<s:param name="homeTown" value="lanzhou"/>
</s:url>
<p><s:property value="test"/></p>
and the result is /path/to/action/Home.action?departYear=2006 and if i change the second parameter <s:param name="homeTown" value="123456"/>, then it shows me properly with /path/to/action/Home.action?departYear=2006&homeTown=123456 I havn't tried to print the url and it works always fine with more parameters. it doesn't triggered any problem at all. I just curious, why it doesn't work with s:property.
btw, i changed the order of 2 parameters too, and it doesn't show the parameter in the url either.
Thanks in advance.
Refer to the documentation... it says that value is an Object, not a string.
<s:param name="homeTown" value="lanzhou"/> would call the getLanzhou() method of your action class to try and populate the value.
What you want is <s:param name="homeTown" value="%{'lanzhou'}"/>
<s:param name="departYear" value="2006"/> works because non-String attribute types are not parsed, but evaluated directly as an expression.
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.
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:
class="current"</s:if>>XXX
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)">