Dynamic delete on jsp file - java

i want to remove some table lines dynamicaly using struts2 and ajax.
i got an object "object1" which contains a list of "object2"
public class Object1 {
[...]
private int id;
[...]
private List<Object2> objs2;
[...]
}
in a jsp file a display informations about object1 in a form to be able to modify some input.
To display the list i use an iterator.
<s:iterator value="object1.objs2" status="objsStatus">
this work fine, and i want to be able to delete one of these object2. so i put a link after each object2 which call a "delete" action with struts2-jquery.
<s:url id="delObject" value="/deleteObject2.action"/>
<sj:a id="delObj%{#objsStatus.index}" href="%{delObj}?object2.name=%{name}" targets="result">delete</sj:a>
my struts.xml
<action name="deleteObject2" class="Object2Action" method="deleteObj">
<result></result>
</action>
this also works fine BUT (there always a but...) i have to "refresh" manually the page to see the "new" list of object2. is it possible to call a javascript function on result success to remove the appropriate table line, using for example:
$(this).closest('tr:not(:only-child)').remove();

You can try Reversen AJAX in jQuery. It will push data when it changed on server.
http://blog.jamieisaacs.com/archives/404
http://plugins.jquery.com/plugin-tags/reverse-ajax

Related

In struts2 framework, DTO consists of list<String>. Its not getting populated from S:select tag

I am new to struts2 and looking at the existing code and elsewhere in the net, I thought my below code should work. I am trying to select some user ids in s:select box in JSP and I want to use these ids in my action class. I am using modelDriven for it and users are in a list
JSP code snippet
<s:select id="selectedAgents"
name="selectedUserList"
multiple="true"
list="selectedUserList"
/>
the selected user ids in the page comes to this box. and when the form is submitted, I expect to see the selectUserList in action.
In the action I have
public class WorkLoadReportAction extends GenericAction implements ModelDriven<WorkloadReportDTO>
...
private WorkloadReportDTO userReportInputData = new WorkloadReportDTO();
...
#Override
public WorkloadReportDTO getModel() {
return userReportInputData;
}
the WorkloadReportDTO has List<String> selectedUserList and its getter and setter.
Now in the method of the action(called from submit), I dont see the selectedUserList populated.
What am I missing?
In your Jsp :
<s:select id="selectedAgents"
name="selectedUserList"
multiple="true"
list="selectedUserList"
/>
You have name attribute value and list attribute value as same . Try changing the value of either name attribute or list attribute .
Hope this Solves your problem

populating a select tag using struts2 jquery plugin

I have a jsp that contains a struts2-jquery-plugin select tag that loads its data dynamically by sending an ajax request as follows:
<s:url var="remoteurl" action="providerList"/>
<sj:select href="%{remoteurl}" id="provider" name="language"
list="pList"
listKey="myKey"
listValue="myValue"
emptyOption="true"
headerKey="-1"
headerValue="Select" label="Provider"/>
Now when ajax request is made, control goes to action codes as follows:
ArrayList<String> pList=new ArrayList<String>();
public ArrayList<String> getpList() {
return pList;
}
public void setpList(ArrayList<String> pList) {
this.pList = pList;
}
public String providerList() {
pList.add("ASC");
pList.add("asas");
pList.add("asasasas");
return "returnedList";
}
In my struts.xml, correponding to actiion providerList, if i specify the result type as dispatcher then it takes the control to a jsp. What i wish is to just make the list pList reach the dropdown list of select tag.
Now the problem is that i want my select tag to be populated with this pList values. How should i configure this providerList action in struts.xml file. or i do i also need to make any changes in my action method ,in return type or anything. As far as i know i can use return type as json . Is there any other solution except json.
First, there is no other solution than JSON, if you're relying on that tag.
In order for the sj:select tag to work, you have to remove the below from the sj:select
listKey="myKey"
listValue="myValue"
Since, what you're sending from action is a list of string, but in the JSP it's expecting a map or a bean because of the above two properties.
In struts.xml, you have to configure the result type to be json and in order to do that we have to include struts2-json-plugin in the project.

Struts 2:How to load values to a <s:select >from a list in session

I am new to struts. I want to load a list of data in the session to a select tag <s:select> which equals to pure html <select><option>values..</option></select> . The data might be loaded from database and put them to a list. I looked for Internet. But it all didn't work for me. Please any one let me know how to do this or provide any link with working example.( including the action class,struts.xml and jsp page. most needed codes are enough.)
As long as you have the list of values in a java.util.List on the stack, you should be fine with something like this:
<s:select label="Some label"
list="yourList"
name="somName" />
You can find a sample here: http://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/
I am not sure why you want to place List in the session?
Struts2 provides a clean way to put your request/response data in Valuestack and its OGNL system provides a very clean way to access those data from the value stack.All you need to have a list in your action class with its getter and setters and at UI use build in struts2 tag to access those data.Here is a simple code to accomplish this
Action Class
public Class MyAction extends ActionSupport{
private List<String> myList;
//getter and setter for myList
public String execute() throws Exception{
myList=new ArrayList<String>();
// fill the list
return SUCCESS;
}
}
At UI level you need to use S2 select tag like
JSP
<s:select label="MyList"
name="myList"
headerKey="-1" headerValue="Select Value"
list="myList"
/>
This is all you need to do. For mapping this in struts.xml its quite straight forwards and all you need to configure your action name and its respected class.Hope this will help you.
For more details about S2, i suggest to refer official doc.
Still if you want to put the list in session in your java class and want to access it in jsp here is the JSP code
%{#session.MyList}
Struts 2.3.1 Documenation

Spring + JQuery dynamic binding

I am new to Spring and still learning. I want to make some more advanced form handling.
Currently my problem is dynamic list binding.
I want to have one text box, one list and add button. What is scenario?
User populates text box(with autocomplete) and cliks add button. After initiation add action, list gets populated without issuing request to server.
User adds few more items to list, and then submits form to server.
What is problem?
I dont know how to bind list or pass dynamic data to server.
Currently I have managed to get JSON response from Controller with list for autocomplete.
Is Spring forms suitable for this task? What is the right way to implement this?
Here's a stab at what I think you're trying to achieve. First: I'm assuming the issue isn't autocomplete/add to list, but rather what to do with the list in the MVC side. Let's say your command object has a property "employee names," defined as
List<String> getNames(){..}
void setNames(List<String>){..}
On the JSP side, you define the form list items like so:
<form:form>
<c:forEach items="${command.names}" var="name" varStatus="status">
<form:input path="names[${status.index}]" />
</c:forEach>
</form:form>
The real trick to making it "dynamic" with jQuery is to add to the form with the next increasing index. So somewhere you have:
<script type="text/javascript">
var count = ${fn:length(command.names)};
function addToList()
{
// add to form with name to "names[count]"
count++;
}
</script>
Putting it all together, you set the list in the controller formBackingObject to an AutoPopulatingList
That should be enough to get you started.

Does ServletRequest.setAttribute permit key names with periods?

I have a java webapp with a Struts 1 action that has the following code:
request.setAttribute("cat.sound", "meow");
On my jsp page, I have the following tag:
<c:out value="${cat.sound}" />
However, "meow" is never printed on the JSP page. This might work if I had an object of type "cat" to do something like:
request.setAttribute("cat", cat);
Unfortunately, this webapp does not have any object defined for the cats and the jsp pages are frozen (no changes allowed).
So is it possible to use request.setAttribute with a key name containing periods/dots? How would the JSP page need to reference the set parameter?
You can avoid having to create a Cat class if you set cat to a map with a String key "sound":
request.setAttribute("cat", Collections.singletonMap("sound", "meow"));
Collections#singletonMap() gives you a nice, succinct way to create a map with one entry.

Categories