Array of objects in Javascript - Struts Forms - javascript

This is for a web application using struts.
I have an array of objects in my Form that gets listed in a table. A user can add/edit/delete from the table. How would I send the changed table back to the Action class?
Will I need to create a string or array of strings, and parse that into an object? Is there a way that java/struts handles objects that are to be modified in the jsp? Or does this need to be cared for in javascript?

Struts binds the request parameters onto the ActionForm object based on name of the input.
actionFormObj.setBla(String x) { ... } matches <input name="bla"... in the form.
When you have related inputs, you can use maps or arrays for ActionForm properties and Struts is smart enough to treat them as well. See here.
Additionally, if your table contains read-only data that you switch to input when editing, you might have to deal with a lot of hidden fields in your form. If you still consider JavaScript as an option, you could create a POST request based on a JavaScript object (that you create with whatever data you wish from the table) and then use jQuery to send it. See here.

Indexed properties in struts, apparently takes care of this.

Related

Alfresco custom UI controls - Associations

I'm trying to build a custom UI control in alfresco to display the associations of an object type that I have.
Basically I have two object types; Code, which is a key value pair, and CodeScheme which contains multiple child associations to codes, it's essentially a mirror of a map structure I have in a different system.
The problem I have is that the codes are automatically generated, so they get the UID names, whereas really I'd like to present them as 'key=value', 'key=value', etc (ideally I'd like to present it as a table).
I've already created a custom control and added it to share-config-custom, and confirmed that the configuration is working correctly. What I'm not really clear on now is:
a) How to attach a javascript to the control so that I can process the association data.
b) How to get hold of the codes in javascript, and read their properties.
I'm just looking for a push in the right direction.
Thanks :)
One idea would be to use a form filter. Your form filter could iterate over the child references, fetch each child node, grab the data you want to display and then add one or more new properties with that data.
Then, your form control is hooked to the fields your form filter dynamically added to the form data. It can then read and display the data as needed.
Without a form filter I think you'd have to use JavaScript to parse the child association refs and use AJAX calls to fetch each child's node data, then format that as needed. The form filter idea would be less traffic from the browser.

Prepend a string to all inputs when serializing a JQuery form

I have an asp.net app that does ModelMapping from a submitted form. Today, I serialize the form and everything works great. I'm planning on nesting the current data structure (that the form maps to) within a class but the problem is that the class's name can vary.
Today I use Jquery's Serialize command on the form but now I need to prepend the new class name (and a '.') to the various inputs in the form.
Is there a simple way to tell Serialize to prepend a variable name? Alternatively, does anyone know an easily prepend a class on a set of variables that are serialized?

Spring-mvc + Thymeleaf: dealing with complex form

I'm working on an internal tool using spring-mvc and thymeleaf.
A section of this tool is used to create an entity we save in the database.
This entity is quite complex; it contains many properties and relations. Some of these relations contain list and other properties.
I have 2 constraints:
Single page. No "wizard".
To only save a completed object in the database.
Now, I'm not really asking for a specific issue. I know my way around thymeleaf, spring #ModelAttribute, etc.
My question is mostly which strategy are you choosing or how to deal with really complex object creation.
Now I can see 3 ways to do it :
Rendering page with thymeleaf. Every time a new element need to be added to a list, I use Ajax to add the new element on the server and rerender the specific fragment. So doing back and forth to the server with my #ModelAttribute and only save at the end.
Rendering a basic page with thymeleaf. Using JavaScript to create html elements and instead of submitting to a #ModelAttribute, I'm serializing my form to JSON and submit this JSON to the server. (kind of client side model)
Rendering a basic page with thymeleaf. Create the html element dynamically with JavaScript when I need to add list item (being sure I'm putting proper name="" to fit with my Java form object) and submit the whole thing at the end.
I'm personally unsure between 1 or 2.
I feel dealing with complex object is much more easier using JSON than form submission. Also, the input value/field with sub object and property can be quite nasty. Having this kind of syntax
does not sound great to me...
3 can probably work but the way spring data binding is done with sub property is lacking some detail in my humble opinion (section 7.4.1 - http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html).
What do you think ?
Personally I use Thymeleaf's own dynamic field management to ensure clean addition of objects and fields to object.
So I will recommend option 4: Dynamic Field management by Thymeleaf.
Have a read of http://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html#dynamic-fields.
I use that for both single field additions as well as addition of nested forms. Does the trick no questions asked.
Hope that helps.

How to Pass ArryList or Array of Objects From C# to Javascript?

I am creating list using linq in c# as following way and it works fine.
lst = (from n in dbEntity.Popup_Notes
select n).ToList();
Now, I want to use this list from client side as array of objects. When user clicks on links, it will display specific notes descriptions as per name selection.
EDIT
i want something like this..
I think you need convert your list to JSON and return to client-side. At client, you can use knockout.js to bind data to your control, this way is easier than binding manually.
Take a look at JSON.NET
Serialize your object(s) to JSON to pass it to your JavaScript code.
In your javascript code use jQuery.parseJson to parse JSON text and build javascript object(s) according to the received JSON text.

How to handle non-trivial form input? JSP or Javascript?

I'm working on a Spring MVC app, which should let user alter contents of a list.
The list consists of Book-objects with simple properties like name and author. The view is a JSP page that displays the list of books and lets the user alter the contents.
Altering the list can mean adding books, removing them or changing the order of the books in the list.
Question is, how do I get the altered list back to the server? I can write JavaScript to control the list, but how do I post it to the Spring controller? On the other hand I can write a JSP form for altering the model which would be trivial to submit back to the server, but am I then limited to basic text fields in form input?
EDIT:
In JSP it is very easy to alter a single model's properties using a form like
<form:form action="myaction" method="post" commandName="mybook">
but if your model is a (ordered) list of objects then how do you edit it?
In Javascript I can get the list of objects from the response and change it as needed, but how do I submit it back to the server? Something like
$.post("/modifybook.do",{ name: "Spring in Action", author: "Graig Walls" } );
works, but only for single objects.
Having never used Spring this may well not be appropriate but could you not convert the list into XML and then post that back to Java and then work on it retaining all intricacies and changes?
You should avoid manipulating the whole list, period. I can't see a scenario where populating entire list of items and sending it back to the server is desired, when only one list element is being changed (edited, added or removed).
What I usually do in my app is I create a handler (controller) for returning a whole list of objects in one go and then add another handlers for adding, editing and removing single entries within that list. I also try to stick to REST in such scenarios, so that I have a clean API which represents server resources and the frontend (AJAX + jQuery) makes use of it.
This solution works really well for me so I suggest sticking to it as well.

Categories