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.
Related
I have trouble to understand how to stop Spring from initializing the child member object of a form bean if none of his fields were modified by the corresponding form. For example I have an User Object that has a field Address, I pass the empty User instance to the form (the Address is null at the time).
In the form I have input fields for all User as well as Address fields:
<form action="#" th:action="#{/user/add}" th:object="${user}" method="post">
...
<input type="text" th:field="*{address.street}"/>
<input type="text" th:field="*{firstName}"/>
...
when I set a breakpoint in the corresponding controller method, the Address object exist within the User object and all its fields are null,0,false(default values). I know the initialization occurs in the WebDataBinder. How can I instruct it not to do so?
The same happens with the dynamic lists, if i had List then the List would get a new entry after form submit.
Would really appreciate any help
You need that object instantiated or you could not store the values entered in the form anywhere.
If you don't want to save the empty object to the DB then you probably need to check it on modification before saving.
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.
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()
Is it possible to access fields defined in Beans/Forms used within an action from a JSP page?
At the moment I can use this:
<s:action name="actionName" var="foo" executeResult="false" />
and access any fields defined in that action class using
<s:property value="#foo.bar" />
but this doesn't seem to work for values defined in a bean or form, which I would normally be able to access using the property tag in the result JSP page for an action.
Just tested this with Struts 2 version 2.3.4.1 and it worked for me to get the displayName value of the kuPerson bean. Note in my Struts action class I have a getKuPerson method that returns an object of type KuPerson and that KuPerson class has a getDisplayName method that returns a String.
<s:action name="person" var="personAction" executeResult="false" />
Display name: <s:property value="#personAction.kuPerson.displayName" />