Struts2 Jquery plugin chaning dropdown values holding on JSP submission - struts2

My question is regarding struts2 jquery plugin for chaining select boxes. I mean by default when the page jsp gets loaded values are already loaded in the dropdown. When we select a value from the first dropdown, based on the selected value the second dropdown is automatically populated through JSON. Now, I have added an onChange event for the second dropdown, on which the page gets submitted and fetch some results on JSP. But in this case the second dropdown is not holding its selected value. The dropdown events call MyAction.java (This class contains all the getters and setters for the properties used for dropdowns) and on change event for the second dropdown will call DisplayResultsAction.java ( This is entirely different action). Could you please let me know what could be the mistake in my code? Your help is greatly appreciated.
my JSp:
<tr>
<td>
<s:url id="remoteurl" action="loadAllZonesAction"/>
<sj:select
href="%{remoteurl}"
requestType="POST"
id="zoneList"
onChangeTopics="getAllDistrictsByZone"
onSuccessTopics=""
name="zoneId"
list="zones"
listKey="zoneId"
listValue="zoneName"
headerKey="-1"
headerValue="--Select--"
/>
</td>
<tr>
<tr>
<td>
<s:url id="remoteurl" action="loadAllDistrictsAction"/>
<sj:select
href="%{remoteurl}"
requestType="POST"
id="districtList"
name="districtId"
formIds="%{frmName}"
onchange="showCenterDetails()"
reloadTopics="getAllDistrictsByZone"
onSuccessTopics="districtId"
value="districtId"
list="districtList"
listKey="districtId"
listValue="districtName"
headerKey="-1"
headerValue="--Select--"
/>
</td>
<tr>

Related

Getting multiple drop down list value from view using JSF 2.0

I am trying to use JSF to retrieve multiple drop down lists according to user input from the view to controller.
However seems I cannot find the correct way.
As the drop down lists are generated dynamically, I cannot hard code the id / name of the drop down list.
Here is my code in the view:
<ui:repeat var="file" value="#{uploadBean.filesInZip}" varStatus="status">
<tr>
<td><h:outputText value="#{file.name}" /></td>
<td>
<h:selectOneMenu value="#{uploadBean.studentSelections}">
<f:selectItems value="#{uploadBean.students}" var="student"
itemLabel="#{student.firstName}, #{student.lastName} (#{student.userId})"
itemValue="#{student.id}"/>
</h:selectOneMenu>
</td>
</tr>
</ui:repeat>
So could anyone give a hand on it?
You're creating multiple <h:selectOneMenu> with the same value.
What you could do is create a HashMap<Integer, T> of your uploadBean.studentSelections, where each <T> match one file in your zip.
Then you can use <h:selectOneMenu value="#{uploadBean.studentSelections[status]}">
You can of course reuse uploadBean.students to populate each of your <f:selectItems>.

struts2 iterator tag - iterating over a collection of collections

I am new to struts 2 and am losing my mind over iterating a collection of collections in my JSP using the iterator tag.
My action class exposes a List (parent) which in turn contains 4 more lists (children). Each child list contains 5 domain objects (e.g. User).
In my JSP, i need to display the User.Name after iterating over the collections. I am able to iterate over the parent collection but cannot get to the child lists. They are anonymous lists and are not exposed by a specific name (i.e. a getter is not available).
<s:iterator value="usrList" var="refParent">
<ul>
<s:iterator value="#refParent.columns" var="usr">
<li>
<s:property value="#usr.Name"/>
</li>
</s:iterator>
</ul>
</s:iterator>
The outer iterator results in 4 <ul> tags but each of those tags are empty i.e. none of the <li> tags are displayed.
All the examples that i see are accessing specific named Collections (e.g. User.PhoneList) but none of them seem to demonstrate this particular behavior.
Any help is greatly appreciated
I think i finally figured it out from one of the Iterator-tag examples (thanks to Umesh and Quaternion). I totally missed that one and did not read through the entire example using top.
<s:iterator value="usrList">
<ul>
<s:iterator value="top">
<li>
<s:property value="Name"/>
</li>
</s:iterator>
</ul>
</s:iterator>
Please try this example from this link
<table>
<s:iterator value="allCities" id="cities">
<s:iterator value="#city.phoneNumbers" id="number">
<tr>
<td>
<!-- Accessing the city name through the city variable -->
<s:property value="#city.name" />
</td>
<td>
<!-- Accessing the objects inside the phone number object through the number variable -->
<s:property value="#number.prefix" />
</td>
<td>
<s:property value="#number.rate" />
</td>
</tr>
</s:iterator>
</s:iterator>
</table>
Struts2 Iterator tag a simple enough and work in a very simple manner.So if you have Collection of Collection or what ever structure think how you will iterator them in a simple Java application , same will apply for the S2 iterator tag, but at same time it will give you more flexibility as well configurations.
In Short you need nested iterator tag.When S2 iterator tag iterate objects it will place current object on the top of value stack and you can easily access that tag in your JSP code.
Since in your code Top level object will be a Collection you need to iterate that again like
<s:iterator value="firstCollection">
<s:iterator value="firstCollection>
/ do what you want here
</iterator>
Since while iterating object will be at top of stack you can refer to that tag directly without any reference.
i Suggest you to have look at following official documents
iterator
iterator-tag-examples

How to retrieve table row value in Struts2 action

I am new to Struts 2 , please help me with the below scenario
I have a table in Jsp using struts2 tag , All I Want to do is to retrieve the selected(checked) table row's value in the action class.
Please let me know how to achieve it .
<s:iterator value="listOfDtos" status="stat">
<tr>
<td><s:checkbox name="delete" value="select" /></td>
<td><s:property value="FirstName"/></td>
<td><s:property value="LastName"/></td>
</tr>
</s:iterator>
<s:submit id="delete" value="delete"/>
}
"listOfDtos" is an array list od dto and is set in the action like the below
ActionContext.getContext().getValueStack().set("listOfDtos", listOfDtos);
Now when the user selects any row to be deleted I want to be able to first of all retrieve this list of dtos in the Action and iterate them to check which of the dtos have the attribute "select" set to true. select" is an attribute of type String in the dto.
How can this be done? Also how will the "listOfDtos" be made available in the Action.
I am not sure i have understood your question properly.but here are few inputs to start with.
There is no need to do ActionContext.getContext().getValueStack().set("listOfDtos", listOfDtos); as this will bind your action class with framework tightly.I suggest you to create a property in your action class with name listOfDtos as ArrayList and provides its getter and setters, framework will place your array-list in the value-stack for you.
You need to provide some unique values to your check-box based on which you can check in your action class which id has been selected as you can perform any desired operation on the ArrayList based on the ID.

update the nested object within struts2

With struts2,it will automaticlly create the bean in our action through the ognl,it is easy to use it when we create a new object,for example,in user register page,we build a form for use to fill their information,and in the action we get the properties populated bean,we can save it directly(maybe need some validation),it is so easy.
But how about update a existed object in the db?
We can build a form,(prepopulate the properties that can be updated in the page),when user make some modifination,we update it.
But the bean we get in the action is created by struts2(we call this bean as bean_struts2),and what we want to updated is in the db(we call this bean bean_db).
Now the problem is how to copy the modifinations of the bean_struts2 to bean_db?
For a non-nested object,we can iterator all the properties of the bean_struts2,and set it to bean_db.
For example:
bean_db.setPassword(bean_struts2.getPassword());
.....
But it does not so easy when the bean to be updated is a nested object.
In my case,these are the entities:
class Task{
Object non_updatedProperty;
List<TaskSteps> steps;
List<User> managers;
User publisher;
}
class User{
id,name;
}
class TaskStep{
id,name;
List<Operator> operators;
TaskStepStatus status;
}
enum TaskStepStatus{
ongoing,completed.
}
class Operator{
id,name;
}
The bean to be updated is the "Task" object.
In the page:
<s:form action="task_update" namespace="/common" cssStyle="width:95%">
<s:textfield value="%{task.id}" cssStyle="display:none" name="task.id"></s:textfield>
<s:textfield value="%{task.publisher.name}" readonly="true" label="Publisher"></s:textfield>
<s:textfield name="task.name" value="%{task.name}" label="Task Name"/>
<table>
<caption align="left">Steps</caption>
<tr>
<th>Step Name</th>
<th>Operators</th>
<th>Status</th>
<th>Set order</th>
<th><span id="addStep" style="cursor:pointer" >Add a step</span></th>
</tr>
<s:iterator value="task.steps">
<tr class="step">
<td>
<s:textfield name="task.steps[0].name" value="%{#this.name}" theme="simple"/>
<s:textfield name="task.steps[0].id" value="%{#this.id}" theme="simple" cssStyle="display:none"/>
</td>
<td><s:textfield name="task.steps[0].end" value="%{#this.end}" theme="simple"/></td>
<td>
<s:select list="allOpIndb" listKey="id" listValue="name" value="%{#this.operator.{id}}"
name="task.steps[0].operator.id" multiple="true" theme="simple" id="a">
</s:select>
</td>
<td>
<s:select list="#com.zjswkj.location.entity.TaskStepStatus#values()" theme="simple"
name="task.steps[0].status" listValue="cnValue" value="%{#this.status}" id="b"/>
</td>
<td>
<span class="up">up</span>
<span class="down">down</span>
<span class="del">del</span>
</td>
<td></td>
</tr>
</s:iterator>
</table>
</s:form>
Through struts2,I can get the bean_strtus2(Task),its id is the same as the bean_db.
I can easily copy its non-nested propertis to the bean_db. but how about the propertis of its TaskSteps and the operators of its TaskSteps.
Just use :
task_db.setTaskSteps(task_struts2.getTaskSteps());??
Of course not,this may cause some new TaskStep with the same name persisited to the db.
Also,there are some properties can not be updated,for example,the Task's "non_updatedProperty" property can not be updated,it can only be set when the task are created.
So in the update form,I did not provide a UI to update this property,so if we call:
task_db.setNon_updateProperty(task_struts2.getNon_updateProperty);
will miss the exist property,beacuse
task_struts2.getNon_updateProperty==null; !!!
I just wonder if there are some good ideas to handle these types of update?
I am not exactly sure if you guys know what I am meaning,but I have try my best to express it.
If there are more questions,I can update it still.
About the question of transferring efficiently informations from a "structure" (a nested set of beans) managed at the presentation layer (so to say here, populated by Struts2) to another "structure" at the service or the "DB" level, I suggest to have a serious look at http://dozer.sourceforge.net
About the problem of passing on value modification of a "structure" from then to the DB through the presentation layer, it highly depends on the way you map your DB tables (I suppose you're using some kind of relational database) to objects.
When using Hibernate for instance, it is sometimes possible to simply create POJO straight out from your presentation beans, and tell hibernate to "store it"; Hibernate would create / update / delete appropriate records in the table according to the passed structure.
Although most of the times there is no silver bullet, and some kind of structure comparison between the structure mapped from DB content and the structure mapped from the presentation layer (through dozer for example) is required to guess what has to be added, modified or deleted.

Struts2: Set values in the bean class onclick checkbox of each row in table

In my struts2 application I have an iterator and a list with objects like -
<s:iterator value="listOfObjects">
<tr>
<td><s:property value="item" /></td>
<td><s:property value="category" /></td>
<td><s:property value="quantity" /></td>
<td><s:property value="brand" /></td>
<td><s:checkbox name="deleteRow" onclick="submit()"/></td>
</tr>
</s:iterator>
The above creates a table with a checkbox in the end column of each row. Now, Onclick any checkbox I want to set the values of corresponding row (only) in my bean class. I tried a lot but I am getting values of each row rather that values of only that particuler row whose checkbox user has clicked. Is it possible. If yes then please help. Thanks in advance.
If the value of the deleteRow checkbox is good enough (based on the toString method of your object), then you should be able to just create a deleteRow property in your Action class with a setter method and parse it out.
I suppose you could set a value expression on the deleteRow checkbox, either using the toString() of the current object, or putting some kind of formatted list (comma separated or something) of the values in there by hand. If you gave the s:iterator an id, you could reference that in the value attribute of the s:checkbox.

Resources