PDA

View Full Version : Dynamic Line Items - (PropertyEditor)


tnist
Nov 23rd, 2004, 02:46 PM
I have been working on trying to figure how best to implement a page where I have dynamic list binding. It seems as though I may need to implemanet a custom PropertyEditor to handle this, but I'm not quite sure.

I have the following domain objects:


public class Claim {
private Long trackingID;
private Long transactionID;
private Date dateCreated;
private Date dateReceived;
private String employeeID;
private String employeeName;
private String claimID;
private String formType;

private List lines = new ArrayList(); /** Detail Line(s) associated with claim */
}

public class ClaimLine {
private Long id;
private int lineNumber;
private Date serviceDate;
private String category;
private Double amount;
private int eligibilityCode;
private String eligibilityComment;
private int duplicate;
}





The Web UI allows the user to insert row via JavaScript and DHTML into the table that conform to the following:
lines[n].serviceDate
lines[n].amount
lines[n].category
lines[n].eligibilityComment
lines[n].eligibilityCode

where n is the next element in the collection to insert.

When I post the form back, the bindAndValidate() method is invoked on the BaseCommandController and throws and exception the following exception:

org.springframework.beans.InvalidPropertyException : Invalid property 'lines[7]' of bean class [mycompany.com.claims.model.Claim]: Index of out of bounds in property path 'lines[7]'; nested exception is java.lang.IndexOutOfBoundsException: Index: 7, Size: 7
java.lang.IndexOutOfBoundsException: Index: 7, Size: 7
at java.util.ArrayList.RangeCheck(ArrayList.java:507)
at java.util.ArrayList.get(ArrayList.java:324)
at net.sf.hibernate.collection.List.get(List.java:217 )
at org.springframework.beans.BeanWrapperImpl.getPrope rtyValue(BeanWrapperImpl.java:522)
at org.springframework.beans.BeanWrapperImpl.getNeste dBeanWrapper(BeanWrapperImpl.java:423)
at org.springframework.beans.BeanWrapperImpl.getBeanW rapperForPropertyPath(BeanWrapperImpl.java:401)
at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:580)
at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:720)
at org.springframework.beans.BeanWrapperImpl.setPrope rtyValues(BeanWrapperImpl.java:747)
at org.springframework.validation.DataBinder.bind(Dat aBinder.java:240)
at org.springframework.web.bind.ServletRequestDataBin der.bind(ServletRequestDataBinder.java:173)
at org.springframework.web.servlet.mvc.BaseCommandCon troller.bindAndValidate(BaseCommandController.java :294)
at org.springframework.web.servlet.mvc.AbstractFormCo ntroller.handleRequestInternal(AbstractFormControl ler.java:236)
at org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(AbstractController.java:128)
at org.springframework.web.servlet.mvc.SimpleControll erHandlerAdapter.handle(SimpleControllerHandlerAda pter.java:44)
at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:522)
at org.springframework.web.servlet.FrameworkServlet.s ervice(FrameworkServlet.java:321)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:810)


The exception is expected since the current model (command object) Claim does not contain the appropriate number of entries in the Lines collection.

How do I implement a PropertyEditor in initBinder to have access to the Claim (command object), insert 1..n new claim lines and then invoke the standard bind? Or can I do this?

Thanks in advance for you assistance.

Regards,
Todd

Armen
Nov 25th, 2004, 05:52 AM
In initBinder(..) method you must add code like that (pseudo code):

if <request> has "ClaimLineId=7" and lines.size<7 than
lines.add(new ClaimLine());

I don't know how you create dynamic lines in jsp (i've used javascript) but
I have to add more verifications, because there are availability not only add, but to delete too. If you want I could send you my class for handling this. (lazzy for typing :P ). Just let me know your address

daniele.guarneri
Nov 25th, 2004, 06:02 AM
Armen, can you post an example in this topic ?
If it is not possible, can you send me a mail with a source code (class and view with javascript) ?

Thanks

PS: my mail --> d_guarneri@virgilio.it

Jurijus Jarmakas
Nov 25th, 2004, 08:27 AM
I am allocating empty objects in formBackingObject, and it works for me. Of cource I should know the size. I have "hidden" input on the web page which has number of items.

Armen
Nov 25th, 2004, 11:01 AM
formBackingObject calls before form is shown. But I want have right size of list after bind and submit. (In session, formBackingObject doesn't call second time)

tnist
Nov 26th, 2004, 07:58 AM
Armen,

Thanks for the reply. Boy that is much simpler then I thought it was going to be, guess I was making it a lot harder then necessary.

Thanks again.

Regards,
Todd

rosco
Dec 3rd, 2004, 03:43 PM
What is the solution to this issue?

We don't know the size of the post'ed collection before its posted.

Thanks,

-Rosco

Armen
Dec 6th, 2004, 05:36 AM
I suggest in initBinder() knowing request parameters (for example if
we have in request param: dynamicLine[5].name=someValue) just add in collection extra new objects (in example at least 5)

rosco
Dec 6th, 2004, 09:05 AM
How do you suggest I get the command object in the initBinder() method?

Armen
Dec 7th, 2004, 03:53 AM
Ok :P you could do it in formBackingObject, or just save reference in formBackingObject

sherihan
Jan 27th, 2005, 02:33 AM
Hi Todd,
I've read your message and I'm having the same problem.
If you were able to solve it would you plz tell me how.
If possible, could you plz send me the html page and the javascript in which you add new row to the table.
My email is: smagied@4s-systems.com
Thanks in Advance.
Sherihan.

etschreiber
Feb 16th, 2005, 11:14 AM
The best way to get the command object is to overide


org.springframework.web.servlet.mvc.BaseCommandCon troller
.createBinder(HttpServletRequest request,Object command)



instead of the initBinder method. If you read the javadoc for createBinder, it explains how it works.

atrellini
Sep 11th, 2005, 03:55 PM
Hi Todd,

I have the same problem with dynamic list binding. Please, could you tell me how to solve this problem?

If possible, you can send me an email with the solution (form class, property editor, html and javascript code)... i'll be very grateful. My address is atrellini@gmail.com

Thanks in advance,
Ariel.

cyber
Sep 12th, 2005, 02:32 AM
pls email me too, chard@pinoyportal.com

Armen
Sep 19th, 2005, 06:04 AM
Hi everyone! I found out a new more elegant and more universal solution. You could use it to solve your problems (not only dynamic lines but others). It is called AJAX and I use implementaion called DWR. http://getahead.ltd.uk/dwr/ Site have all needed docs. This api have support for spring. :P Ok I explain how use it:
You have commad object Parent which have collection of childs and you want to add new child to collection from web page. So you should call special javascript function which send request to server and then some class for example ChildAdder wilt get Parent from session (you have access to the session using dwr api) and will create and add new Child object. Using such api you could do whatever you want calling java from javascript. Read more info on dwr site