File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes AbstractWizardFormController: teasing command object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "AbstractWizardFormController: teasing command object" Watch "AbstractWizardFormController: teasing command object" New topic
Author

AbstractWizardFormController: teasing command object

Rohit Bhal
Ranch Hand

Joined: Aug 28, 2007
Posts: 44
Hi

Could I use a non-persistent domain object as a command object in spring mvc and have an instance of a persistent(made persistent using @Entity annotation in the domain class) object in it and have its (persistent object) data made persistant in a database?

I am using AbstractWizardFormController for my application and it shows the data of both the persistent and non-persistent domain objects in all the jsp pages in the wizard but when it comes processFinish(..) method to finish the wizard, where i am trying to persist on the data of the properties of the persistent domain object to the database, it gives me a java.lang.NullPointerException.

The situation is like this:

Non-persistent domain object


Persistent domain object


The thing is that i need the "x" variable in the non-persistent object (that I want to use as a command object) for some data to be used on the jsp pages in the wizard but not to have it persisted in the database.

I've posted the complete code of the application I've been trying with all the files being used in it in my previous post at http://www.coderanch.com/t/507667/Spring/AbstractWizardFormController-java-lang-NullPointerException-at.

Could someone help me with query mentioned above?
Thanks


Skip Skipping
Mark Secrist
Ranch Hand

Joined: Jul 01, 2003
Posts: 89
You may have solved your original problem already but wanted to mention that I've had fairly good success using a persistent object as my command object even when I have non-persistent attributes used solely for the controller/JSPs. The way to get this to work is to add the 'transient' keyword to the attribute (i.e. private transient foo).

Mark
Rohit Bhal
Ranch Hand

Joined: Aug 28, 2007
Posts: 44
Hello Mark

I did have it working by having a persistent entity encapsulated in a non-persistent one, but thanks for reminding me of the 'transient' attributes to be used as a better strategy for that.

I think I've forgotten a lot.

Many thanks

Rohit Bhal
Ranch Hand

Joined: Aug 28, 2007
Posts: 44
Hello Mark

I've been having a problem for a while that I am not able to understand. The situation is like this.. I have a page in the wizard that I use for adding business locations (BusinessLocation.java entity) of a service provider (ServiceProvider.java entity) but the thing is that I dont get a consistent result when I see the next page in which I display all that business locations that I just added in the previous page of the wizard. Sometimes I see all the locations displayed but sometimes just the last one added and even the order in which they are displayed is not consistent and I always get a blank location displayed in the list of added locations. I added a blank businessLocation in the postProcessPage(..) method in my wizard controller for populating a blank form after adding a location and also for incrementing the counter by one for moving on to next index in a list (businessLocations[${locationIndex}]).

Here is the code for my controller and jsp:
RegisterServiceProviderController.java


locationDetailForm.jsp



Could you help me understand that why am I not getting all the added locations displayed consistently in a consistent order I am adding them?

Thanks
Mark Secrist
Ranch Hand

Joined: Jul 01, 2003
Posts: 89
Have you implemented your own hash() method for BusinessLocation? If you haven't overridden the hash method, the typical behavior is every object (even ones that are technically equivalent) would have its own entry. This sounds almost like you are having your hash method return the same hash for multiple - non equivalent entries. In terms of the order, HashSet does not preserver order of adding so that's probably why you are seeing the order change. Unless you truly need a unique set of entries, I'd consider using List & ArrayList.

Also, in your JSP, I'm curious where locationIndex is being set?
Rohit Bhal
Ranch Hand

Joined: Aug 28, 2007
Posts: 44
Below is the sample of what is happening. I've shown them step by step for how I am seeing things happening.

Here is what I get when I try the application for 1st time:

1. http://localhost:8080/springmvc/register.htm
2. Number of locations: 4
3. Click 'Next'

I get no parameters or their values printed on the console...

4. Address: A-81, Seema Apartments
State: Illinois
5. Click 'Add Business Location'

I get the following output on console:
----------page:--------------------1----------
----------_target1:--------------------Add Business Location----------
----------serviceProvider.businessLocations[0].state.abbreviatedName:--------------------IL----------
----------serviceProvider.businessLocations[0].address:--------------------A-81, Seema Apartment----------

6. Address: ED-48, Kumaon Hostel
State: New York
7. Click 'Add Business Location'

I get the following output on console:
----------serviceProvider.businessLocations[1].address:--------------------ED-48, Kumaon Hostel----------
----------page:--------------------1----------
----------_target1:--------------------Add Business Location----------
----------serviceProvider.businessLocations[1].state.abbreviatedName:--------------------NY----------

8. Address: A-101, Mahavir Lok
State: New York
9. Click 'Add Business Location'


I get the following output on console:
----------serviceProvider.businessLocations[2].address:--------------------A-101, Mahavir Lok----------
----------page:--------------------1----------
----------_target1:--------------------Add Business Location----------
----------serviceProvider.businessLocations[2].state.abbreviatedName:--------------------CA----------

10. Click 'Next'

I get the following output on registrationConfirmationForm.jsp

locationCount: 4

Address:
State:

Address: A-81, Seema Apartment
State: IL

Address: ED-48, Kumaon Hostel
State: NY

Address: A-101, Mahavir Lok
State: CA


And the same data as shown above get persisted in the database as well in the same order.

Now when I do the same steps for the 2nd time, I get all the data shown on the console just like before but when it comes to registrationConfirmationForm.jsp, this time it shows me the following:

locationCount: 4

Address:
State:

Address:
State:

Address:
State:

Address: A-101, Mahavir Lok
State: CA


Only 1 out of 3 Addresses and States are being deplayed this time.

I updated the postProcessPage(..) in RegisterServiceProviderController.java for having the data entered in the form displayed. Below is the updated postProcessPage(..) method:



Could you help me understand what could be the reason behind it and how to get it resolved?

Thanks
Rohit Bhal
Ranch Hand

Joined: Aug 28, 2007
Posts: 44
Hello Mark
I dont have the equals() or hashcode() overridden, so I am assuming that every object is going to have its own hashcode and unique.
And the locationIndex is being used on line 13 in the locationDetailForm.jsp mentioned below for the path attribute of the select tag.
Rohit Bhal
Ranch Hand

Joined: Aug 28, 2007
Posts: 44
I had the State.java entity changed to as mention below as I was having some issues with it.

State.java


To resolve the issue i was having with persisting the whole chain of object I also had to make the change in BusinessLocation.java entity as shown on line 44 in BusinessLocation.java

BusinessLocation.java


But I dont think that it should be a concern as i am getting the whole chain persisted. It was jus because you might have found an attribute being set in locationDetailForm.jsp but might have not found in the State.java entity was why I though I should have sent my updated Stata.java and BusinessLocation.java.

I wonder what am I missing now?

Thanks for the reply
Rohit Bhal
Ranch Hand

Joined: Aug 28, 2007
Posts: 44
I tried the same steps again as I mentioned above in one of my previous post for the same data and this time after implementing equals() and hashCode() methods using append() and isEquals() methods of BuildEquals() class and append() and toHashCode() mthods of class BuildHashCode() from commons-lang-2.5. jar and this time I got a bit too strange an output.

Output:

locationCount: 4

Address:
State:

Address: A-101, Mahavir Lok
State: NJ


I wonder what am I missing?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AbstractWizardFormController: teasing command object