Call a method passing a parameter inside jsp with struts 2 - struts2

I have a scenario where I need to call a method of an Action class.
Below is the code.
<s:if test="#session['EXECUTOR'] != null">
<tr>
<td width="80%">
Test:
<!-- <s:property value="#moveETHAction_fetchExecutorData(#session['EXECUTOR'])" /> -->
<s:push value="#moveETHAction_fetchExecutorData(#session['EXECUTOR'])">
<s:property value="top">
</s:push>
</td>
</tr>
</s:if>
However it's not working, throwing some Jasper Exception -" According to TLD, tag s:property must be empty, but is not"
How do i call the method and return a String data?

shouldn't it be:
<s:property value="top"/>
in stead of
<s:property value="top">

Related

how to get value of selected link value from list of links?

In my jsp page I have the following code and it can successfully go to the action class once the link was clicked.
My concern is that I need to know in my action class what is the value of the selected link from the list.
<s:iterator value="mediaLendingList" >
<tr>
<td><s:url action="addMediaLending" var="urlTag" />
<a href="<s:property value="#urlTag" />" ><s:property /></a>
</td>
</tr>
</s:iterator>
Can anyone give me an idea on how to do this? Thanks in advance.
Use <s:param> tag to put parameters to <s:url> tag and top keyword to get current element of the iteration.
<s:iterator value="mediaLendingList">
<tr>
<td>
<s:url action="addMediaLending" var="urlTag">
<s:param name="variableName" value="top" />
</s:url>
<s:a href="%{urlTag}"><s:property /></s:a>
</td>
</tr>
</s:iterator>

In s:iterator tag im having set of hyperlinks which leads to respective datas. I need to know which link is clicked

In s:iterator tag I have set of hyperlinks which leads to respective data. I need to know which link is clicked
Here my code:
<s:iterator value="datasFinal" status="reportStatus">
<tr class="<s:if test="#reportStatus.odd == true ">odd</s:if><s:else>even</s:else>">
<td align="center"><s:property value="#reportStatus.count" /></td>
<td align="center"><a href="javascript:submit('')">
<s:set name="index" value="#reportStatus.index" scope="session"/>
<s:property value="reportName" /> </a></td>
</tr>
<s:hidden name="reportName" value="%{reportName}"/>
</s:iterator>
I got the solution, thank u RAO and Dave.
<s:iterator value="datasFinal" status="reportStatus">
<tr class="<s:if test="#reportStatus.odd == true ">odd</s:if><s:else>even</s:else>">
<td align="center"><s:property value="#reportStatus.count" /></td>
<td align="center"><a href="javascript:submit('${reportName}')">
<s:set name="index" value="#reportStatus.index" scope="session"/>
<s:property value="reportName" /> </a></td>
</tr>
</s:iterator>
By submitting this to java script i got which link is clicked...

Iterator, set, if-else tags of Struts2(Getting the value of s:radio and applying if-else )

I have a jsp page in which I have two radio buttons and a select tag. Now, if the first radio button is clicked than I want to make that select tag disable but I am fail to do this , I have tried using "disabled" property. Following is the code.
Jsp Page
<table align="center">
<s:iterator value="FirstObjectList" status="AuthorTypeStatus">
<tr>
<td>
<s:radio name="radio_SelectedValue" list="{ObjectName}" listKey="ObjectKey" listValue="ObjectName" value="DefaultObject"/>
</td>
</tr>
</s:iterator>
<s:if test="%{#radio_SelectedValue == 'ObjectName1'}">
<s:set name="isSelectDisabled" value="false"/>
</s:if>
<s:else>
<s:set name="isSelectDisabled" value="True"/>
</s:else>
<tr >
<td colspan="2">
Select Parent Discover Lab
</td>
</tr>
<tr>
<td>
<s:select name="select_SelectedValue" headerKey="DefaultObject" headerValue="ParentObject" list="ObjectList" listKey="ObjectListKey" listValue="ObjectListValue" disabled="%{isSelectDiabled}">
<s:iterator value="ObjectList">
</s:iterator>
</s:select>
<s:submit value="Continue">
</s:submit>
</td>
</tr>
All the getter and setter methods are fixed..is this possible without javascript?
I'm a little confused by your code block, but I think I understand what you're aiming for. Something like this should work
<tr>
<td>
<s:radio name="radio_SelectedValue" list="%{radioList}" listKey="ObjectKey" listValue="ObjectName" value="DefaultObject"/>
</td>
</tr>
<s:if test="%{radio_SelectedValue.equals('myChosenValueFromTheList')}">
<s:select name="select_SelectedValue" headerKey="-1" headerValue=" " list="ObjectList" listKey="ObjectList.Key" listValue="ObjectList.Value" />
</s:if><s:else>
<s:select name="select_SelectedValue" headerKey="-1" headerValue=" " list="ObjectList" listKey="ObjectList.Key" listValue="ObjectList.Value" disabled="disabled" />
</s:else>
I'm not sure what you're doing with the iterators with these tags, they handle the list propagation themselves. Simply tell the tag what list to use and struts will populate the tags appropriately. You can then do a conditional based on the value of the radio when the page renders and perform magic on the select tag as shown above.

comparing two valuestack string values in JSP - struts2

Thanks in advance for your time.
I need to preselect a radio button if it has a saved value. I basically need to compare 2 strings in the valuestack to determine this.
(I can't use <s:radio at the moment because of some business rules I need to attach based on other input elements in the form).
I tried to do <s:set the value of the saved id inside s:iterate like below and then compare them like below but obviously I didnt get it right.
<s:set var="savedId" value="%{flag.problemId}"/>
<s:iterator value="problemIdList">
<s:set var="currentId" value='<s:property value="id"/>' />
<s:if test="%{#currentId.equals(#savedId)}" >
<input checked="checked" type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/> <s:property value="description"/> <br/>
</s:if>
<s:else>
<input type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/> <s:property value="description"/> <br/>
</s:else>
</s:iterator>
Basically I need to compare the two strings, my code is below. I know I can't compare with equals() like I have below - any ideas?
Thanks a bunch!
<s:set var="savedId" value="%{flag.problemId}"/> <s:iterator value="problemIdList">
<s:if test=' <s:property value="id"/>.equals(<s:property value="savedId"/>) '>
<input checked="checked" type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/> <s:property value="description"/> <br/>
</s:if>
<s:else>
<input type="radio" type="radio" name="problemId" id="problemId" value='<s:property value="id"/>'/> <s:property value="description"/> <br/>
</s:else>
Regards,
VeeCan
Have you tried:
<s:if test='id.equals(savedId)'>
If "id" is a string OGNL will allow you to use methods of String.
Access value form <s:iterator> into <s:if> (ognl with <s:if> and <s:iterator>)
For example: Suppose loadUserList is the iterator(containing UserName and Address) to be iterated in jsp,
UserName:<s:property escape="false" value="userName" />
Address:<s:property escape="false" value="address" />
<s:if test='userName.equals("Admin")'>This is admin User</s:if>
<s:else>This is not an admin user!</s:else>
OGNL with s:if and s:iterator

How do you iterate through a list of objects?

I have a User class that has a String username in it. I have a list of users that I'm trying to display in a table using
<s:iterator value="users" id="list">
<tr>
<td><s:property value="#list.username" /></td>
<td></td>
<td></td>
<td></td>
</tr>
</s:iterator>
The rows are being displayed the right number of times, so it's iterating through my list properly. However, I don't know how to access the username property to display it. Obviously what I have above isn't correct... Any ideas?
First, in Struts2 2.1.x the id attribute is deprecated, var should be used instead (ref)
I think the # is misused there. Besides, "list" seems a bad name for what is to be assigned in each iteration... I think "user" is more appropiate.
IIRC, the syntax is
<s:iterator value="users" var="user">
... <s:property value="#user.username" />
</s:iterator>
Further, you don't need to assign the current item in the iterator for such a simple case. THis should also work:
<s:iterator value="users">
... <s:property value="username" />
</s:iterator>
Also you might want to try this:
<s:iterator value="users">
... <s:property /> <!-- this outputs the full object, may be useful for debugging -->
</s:iterator>
UPDATE: I corrected the bit about the #, it was ok.
You can do like this
<s:iterator value="users" >
<tr>
<td><s:property value="username" /></td>
<td></td>
<td></td>
<td></td>
</tr>
Observe that no need of #list there.
The other way is
<s:iterator value="users" var="user">
<tr>
<td><s:property value="#user.username" /></td>
<td></td>
<td></td>
<td></td>
</tr>
insetead of id give it as var.Since
we don’t specify a scope for var, this new user reference exists in
the default “action” scope—the ActionContext. As you can
see, we then reference it with the # operator.
You can use JSTL with Struts. It has a <c:forEach> tag in its core library that will allow you to iterate through a list or any other collection easily.
<s:iterator value="users" var="eachUser">
<div>
<s:property value="#eachUser.username">
</div>
</s:iterator>
Check this for more such examples
Struts 1.x takes care of your inner properties like this.
< logic:iterate id="user" name="userList">
< bean:write property="${userName}"/ >
< /logic:iterate>
I guess as per ur example u can have. You may not need "#list." again in the value attribute
< s:iterator value="users" id="list">
< s:property value="userName" />
< / s:iterator>

Resources