I have a list of Strings that are attribute names and a Map.
I'm trying to access a model(ex.project) in the map using attribute name in string list.
Here is what I have now.
<s:iterator value="theMap" var="element">
<tr>
<s:iterator value="attributeList" var="attrName">
<td><p><s:property value="#element.Project.#attrName" /></p></td>
</s:iterator>
</tr>
</s:iterator>
If I hard code the attribute name it works fine:
<td><p><s:property value="#element.Project.projectName" /></p></td>
Any advice is appreciated.
Using OGNL <s:property value="#element.Project[#attrName]" />
Related
I'm iterating through a List<SomeObject>, but I want only to print out certain attributes of SomeObject depending on the attributes specified in another List. So far, I have got it. What I'm not sure, how to use the item in the List as the attribute of SomeObject that I want to print out.
<s:iterator value="theList" var="listItem" status="theListCount">
<s:iterator value="listOfAttributes" var="attr" status="attrListCount">
Attribute=<s:property value="#attr"/> Value=<s:property value='#listItem.[%{#attr}]'/>
</s:iterator>
</s:iterator>
No need to force OGNL in the part of the expression
<s:iterator value="theList" var="listItem" status="theListCount">
<s:iterator value="listOfAttributes" var="atr" status="attrListCount">
Attribute=<s:property value="#atr"/> Value=<s:property value="#listItem[#atr]"/>
</s:iterator>
</s:iterator>
First of all #attr is a reserved keyword, so don't use it as a variable value. And use [] notation to reference property of the object.
<s:iterator value="theList" var="listItem" status="theListCount">
<s:iterator value="listOfAttributes" var="atrbt" status="attrListCount">
Attribute=<s:property value="#atrbt"/> Value=<s:property value="#listItem[#atrbt]"/>
</s:iterator>
</s:iterator>
my checkbox is not showing the values of my list as "checkable" things.
Its just saying "publication.keywords" :(
But my list is not empty, as I can show it in an iterator.
Please help me. Whats my mistake?
This is my Checkbox
<s:checkbox key="publication.keyword" list="keywords.{name}" />
Its working with the Iterator
<s:iterator value="keywords">
<tr>
<td><s:property value="name" /><br></td>
</tr>
</s:iterator>
The syntax is wrong.
Looking at your iterator, the checkboxlist should be like :
<s:checkboxlist list="keywords" listValue="name" listKey="name"/>
The 'listKey' attribute specifies what will be submitted, if that checkbox is checked. I have used name for now, you can change it to what you require at the backend.
For e.g. if
List<MyBean> keywords
and
class MyBean
{
String name;
Integer id;
//getters and setters
}
then, the s:checkbox can be something like :
<s:checkboxlist list="keywords" listValue="name" listKey="id"/>
I have lists of objects named allAlbums and allPhotos. Now I want to show all photos in each album so I used the following method. My code is
<s:iterator value="allAlbums">
<s:iterator value="allPhotos">
<s:if test="%{#allAlbums.albumid == #allPhotos.albumid}">
<s:property value="photourl"/>
</s:if>
</s:iterator>
</s:iterator>
But it is not working. Any suggestion what am I doing wrong?
Make sure that both of your object have appropriate getters/setters, then you can use feature provided by OGNL called projection.
<s:iterator value="allAlbums" var="album">
<s:iterator value="allPhotos.{? #this.albumid == #album.albumid}">
<s:property value="photourl"/>
</s:iterator>
</s:iterator>
You are referencing wrong object. Define a var attribute for the records you would reference and reference it instead.
<s:iterator var="album" value="allAlbums">
<s:iterator var="photo" value="allPhotos">
<s:if test="#album.id == #photo.albumId">
<s:property value="photoUrl"/>
</s:if>
</s:iterator>
</s:iterator>
I currently have the following code and the data is displayed fine.
<logic:iterate name="myList" id="product" indexId="iteration" type="com.mycompany.MyBean">
<tr>
<td> <bean:write name="product" property="weight"/> </td>
<td> <bean:write name="product" property="sku"/> </td>
<td> <bean:write name="product" property="quantity"/> </td>
</tr>
</logic:iterate>
But now I need to make the "quantity" part modifiable. The user should be able to update that field, press submit and when its sent to the server, "myList" should automatically update with the new quantities.
I've tried searching for help on this but all I keep finding is examples on how to display data only, not modify it. Any help would be appreciated.
So this is tricky, because there are many things to get done in order for it to work. First, declare your tags inside the iterator with the html tags, with attribute INDEXED=TRUE and an ID DIFFERENT THAN THE NAME, i also took out the "indexId" attribute to use the simple "index" word for the index:
<logic:iterate name="myList" id="myListI" type="com.mycompany.MyBean">
<tr>
<td> <html:input name="myListI" property="weight" indexed="true"/> </td>
<td> <html:input name="myListI" property="sku" indexed="true"/> </td>
<td> <html:input name="myListI" property="quantity" indexed="true"/> </td>
</tr>
after that, in order for struts to be able to get and set the attributes of your beans, you need to declare EXTRA get and set methods inside your collection object, using the name you wrote in the id of the iterate tag. In this case, you would write 2 extra get and set methods for the "myListI" :
public void setMyListI(int index, myBean value){
this.myList.add(value);
}
public myBean getMyListI(int index){
return this.myList.get(index);
}
I think Th0rndikes answer is mostly correct. My implementation is slightly different, so it might be worth trying this as well.
Form
private List<Parameter> activeParameters;
public List<Parameter> getActiveParameters() {
return activeParameters;
}
public Parameter getParam(int index){
return this.activeParameters.get(index);
}
JSP
<logic:iterate name="MyForm" property="activeParameters" id="param">
<tr>
<td><bean:write name="param" property="prompt"/></td>
<td><html:text name="param" property="value" indexed="true"/></td>
</tr>
</logic:iterate>
In summary, I didn't use Type in the iterate tag, using the property tag instead. In the bean adding a getter with matched the name of the iterate ID in the JSP (param) with an index as a method parameter did the trick.
Take a look at this: http://wiki.apache.org/struts/StrutsCatalogLazyList
Indexed Properties
Struts html tags have an indexed attribute which will generate the
appropriate html to populate a collection of beans when the form is
submitted. The trick is to name the id attribute to the same as the
indexed property.
For example the following jsp...
<logic:iterate name="skillsForm" property="skills" id="skills">
<html:text name="skills" property="skillId" indexed="true"/>
</logic:iterate>
...will generate the following html
<input type="text" name="skills[0].skillId value="..."/>
<input type="text" name="skills[1].skillId value="..."/>
....
<input type="text" name="skills[n].skillId value="..."/>
When the form is submitted BeanUtils will first call the
getSkills(index) method to retrieve the indexed bean followed by
setSkillId(..) on the retrieved bean.
Theoretically, the indexed attribute of the struts html tags could be used for this:
Valid only inside of logic:iterate tag. If true then name of the html tag will be rendered as "id[34].propertyName". Number in brackets will be generated for every iteration and taken from ancestor logic:iterate tag.
But, there is no corresponding indexed attribute on the html:errors tag, which limits its usefulness. Also, the required combination of id, name and property attributes can be rather confusing.
I found it easier to use jsp scriptlets to generate the property name including the iteration index. The following code requires that your form has a string array property "quantity".
<% int idx=0; %>
<logic:iterate ...>
<html:text property='<%= "quantity[" + idx + "]" %>'/>
<html:errors property='<%= "quantity[" + idx + "]" %>'/>
<% i++; %>
</logic:iterate>
if anyone knows , how to iterate String list in jsp using in struts2.
(I want to display values of the list in the jsp)
Please help me..
Java
public class YourFormBean{
private List<String> listOfString;
//setter & getters
}
JSP
<s:iterator value="listOfString" status="statusVar">
<tr>
<td><s:property/></td>
</tr>
</s:iterator>
Iterator will iterate over a value. An iterable value can be any of: java.util.Collection, java.util.Iterator, java.util.Enumeration, java.util.Map, or an array.
<table border="0" cellspacing="1" cellpadding="0" width="100%" class="boxtbl">
<s:iterator value="listOfString" status="strVal">
<tr>
<td><s:property/></td>
</tr>
</s:iterate>
</table>
status If specified, an instanceof IteratorStatus will be pushed into stack upon each iteration
Java code:
List<String>days=Arrays.asList("SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THRSDAY","FRIDAY","SATURDAY");
Jsp
<c:forEach items="${days}" var="days">
<option value="${days}">${days}</option>
</c:forEach>