How do I select a specific object from the action class when pulling up multiple objects in JSP.
From my action class I pass through Struts five of the same object. In JSP how would I select a specific object and property of the object to display on page. I have tried putting various values and names in the <s:form> tag but I have yet to figure out how to do it. A push in the corrected direction would be welcome.
Use
<s:iterator value="walkthroughs" >
<s:property value="id"/><br>
<s:property value="areaName"/><br>
...
</s:iterator>
And you should rename your getter methods to be bean compartible.
getId(), getAreaName()
Related
Let's say I have a collections called imagesNames.
In my JSP file, I want to display the first element of this collections using Struts property tag.
This is what I have tried, but obviously this won't work.
<s:property value='imagesNames[0]' />
Obviously this won't work, but you can make it working if you populate the collection and provide the public accessors before you return a dispatcher result to the JSP page.
<s:property value="imagesNames.first" />
I am having a problem in passing Struts2 property tag as parameter in java function in the jsp.
I am calling a java function like this from jsp,
<s:if test="%{DoSomething()}">
DoSomething() function is in the Action class, and is being called for each record in the iterator.
The problem is that I want to pass this property <s:property value="userId"/> in the function DoSomething as java string as follow.
<s:if test="%{DoSomething("<s:property value="userId">/)}">
Can anyone please guide me that how can I do that. I goggled it but did not find anything.
<s:if test="%{DoSomething(userId)}">
Most likely you wouldn't need to pass in a parameter like this. You are trying to call a method DoSomething from your action class. You want to pass in a parameter to this method in your action class. The userId must be existing already in you action class otherwise you couldn't be able to get it through the s:property tag. You can just use this userId variable in your DoSomething method of your action class.
you cannot used Struts2 tag to another Struts2 tag, you can use expression language,
first use <s:set name="userId" value="userId" /> then use ${userId}
I'm looping over a list and want to access the value to create a text input field. Unfortunatley I'm failing to access the loop variable inside the <s:textfield> tag
This is what I tried:
<s:iterator var="tag" value="searchTagList">
<s:property value="#tag"/>
<s:textfield key="searchResults.#tag" name="#tag" value="#tag" />
</s:iterator>
The <s:property value="#tag"/> is evaluated correctly and shows the loop variable. But the #tag in the <s:textfield> is never evaluated.
I also tried to put <s:property value="#tag"/> instead of #tag without success.
The tag's key attribute used instead of three ones name,value,label. As far as two of them you have already defined, you can change
<s:textfield label="%{getText('searchResults.'+#tag)}" name="%{#tag}" />
The value is retrieved by the name attribute. You should provide a getter for the name evaluated in the name attribute. Note, getText() is available if your action extends ActionSupport. And you have to force name evaluation in the name tag.
Hello dear folks of stack overflow.
I encountered a problem recently in my Struts application.
I have a jsp which displays some bean value correctly (i paste only relevant part of the code, i simplified to the extreme):
<table>
<logic:iterate name="bean" property="list1" id="listItem">
<tr>
<td>
<html:checkbox name="listItem" property="selected">
</html:checkbox>
</td>
</logic:iterate>
</table>
My bean has a list1 property with its getter and setter
private List<RandomObject> list1;
public List getList1() {
return list1;
}
public void setList1(List list1) {
this.list1=list1;
}
and my sub-bean has a selected property:
private boolean selected;
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
Now, when enter this jsp, the values i get are correct, ie: if my mapped object in DB was at selected=true, the checkbox is checked. What i dont get is how do i save the changes i make in this form. When i submit, all the changes are lost. This is pure struts related, because in debug when i enter the StrutsAction linked to submit, the ActionForm i get has already lost all interesting values. Also i feel like it has to do with the fact that the value i want to retain isn't directly stored on the main bean, but rather is a property of a sub-bean, because on the same page there are a lot of other properties directly on the main bean that i have no trouble saving.
What did i miss ?
It's probably declaring incorrect name attributes in the rendered HTML. If you look at the generated source code for your page it probably looks like this:
<input type="checkbox" name="selected" ...>
which would look for a selected field in your form bean class.
If you're using a collection that's a field in your form bean you want to be using the <nested:form>, <nested:iterate> and <nested:checkbox Struts tags, not the <html:form>,and` ones. So it would then look like this:
<nested:form action="foo">
...
<nested:iterate property="list1" id="listItem">
<tr>
<td>
<nested:checkbox property="selected">
</nested:checkbox>
</td>
</tr>
</nested:iterate>
...
</nested:form>
Note that I've removed the name attributes from the Struts tags, because they're not needed (in my experience they actually cause all kinds of problems - if you use <nested:*> tags don't use a name attribute). In the case of the <nested:iterate> the level of nesting is the form bean itself, so it knows that it needs to look for the list1 property in the form bean.
That tag creates its own nesting level, so the <nested:checkbox knows that it needs to look for the selected property of the current element in the iteration.
The rendered HTML will look something like this (for the first element):
<input type="checkbox" name="list1[0].selected" ...>
which means the selected field of the first element (index 0) in the collection referenced by the list1 field of your form bean.
And, of course, you'll need to make sure that you're using a session-scoped form bean, rather than a request-scoped one.
in my case i had to put the name attribute into the iterate otherwise it gives me error(cannot find property into any bean...)
I resolved deleting the attribute name into the checkbox: it seems it create a new object at page scope that is not connected with the form.
This is my code:
<nested:iterate id="apertura" type="it.puglia.innova.view.actionform.AperturaForm" indexId="index" name="strutturaRuraleForm" property="listAperturaForm">
<nested:checkbox styleId="checkbox_${index}" property="flagContinuato" onchange="changeOrarioContinuato(${index})"/>
that's it :-) didn't need to change html:form to nested too.
Bye
If suppose, I have a List of type Schedule on my JSP page. I iterate through it and wants to send a particular object of the list to the action class. Is this possible to do that using Struts 2? What I have explored is that I can send the value of the identifier variable of the object to the action class and then fetch the row corresponding to it there.
<s:form action="FlightAction">
<s:iterator value="schedulelist" var="flight">
<s:if test="#flight.sid==10">
<s:hidden name="object" value="#flight"/>
</s:if>
</s:iterator>
<s:submit value="Send"/>
</s:form>
Now what I want is when value of SID is 10, then the whole Schedule object is sent to the action class. schedulelist refers to a list that contains object of type Schedule. SID is the identifier variable of the Schedule POJO. Is sending a complete object in this way possible in Struts 2?
put the "Schedule" object you want to send to action class in request scope, and then you can access it in action class.
<s:set name="scheduleObj" value="%{scheduleList.get(2)}" scope="request" />
The above example puts 3'rd object into scheduleObj request variable.
When you use
<s:hidden name="object.sid" value="%{#flight.sid}"/>
Then you be able to send the value to the action to the object of type Schedule which you have to create and provide getObject(), setSid() methods.
If you want to populate a collection of objects of type Schedule, then you need to use indexing.
<s:hidden name="object[%{#status.index}].sid" value="%{#flight.sid}"/>
Read the Struts Type Conversion: Collections and Map support.