• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Accessing multiple entities with one jsp

 
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been doing a Spring framework course and have done a SpringMVC/Hibernate application example. I decided to get some practice by expounding on that example. The example was CRUD operations on one entity with one controller. I added more entities and some entities have attributes/fields/members that are other entities. I was able to access and display the data for multiple entities in one jsp easily enough. I hit the wall when I tried to create a new entity. Here is my problem:

In my app, I have a "Client" entity. I want to create a new Client, but to do so, I need to also create a new "Address" entity and assign it to the Client's "address_id" column in the client db table. I need to be able to assign the id of an existing "AccountManager" entity in the Client's db table in the account_manager_id column. I also need to create one or more "roles" when the Client is created and assign them to the newly created Client in the Role's db table client_id column. Then there is the whole other matter of setting up the ClientController to handle all of this. I feel like if I understood how I can access these different methods and entities, I would be set. I am feeling so defeated at the moment.

My code is a mess right now because I have been adding, trying, failing, changing... but I will post what I have. So far, I have been able to produce some input fields but they don't hook up to anything because I don't know how to do that with more than one entity, because the course only covered working with one entity with simple string fields.

My create-client.jsp:



My ClientController:



My Client entity:



My "Address" entity:



My Role entity:


 
Saloon Keeper
Posts: 29002
214
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, that's more than my poor strained eyes can read, but you've got some pretty complex EL going on in that page template. It's possible that it's too complex, but I can't say for sure.

One thing, though. You have defined 'label for="name${i}"' type elements on your page. The "for" attribute takes the name of an HTML/XML id attribute and ids are magic. Every id in an XML-compliant document must be unique and you cannot declare or reference them via EL. They have to be hard-coded.

I'm not sure how JSTL handles that in a forEach. JavaServer Faces resolves it by generating unique internal ids from the simple ids in the View Template, so maybe JSTL does also.
 
Ray Gilbert
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, ignore that jsp code, that was a last ditch attempt to get chatgpt to help me. Not recommended. Lol
 
Ray Gilbert
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so I worked on my jsp some:



And my controller methods:







But I'm steady hacking away at it.  I have been at it since 06:30 yesterday morning. I think I'm going to bed now. Lol
 
Ray Gilbert
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I tried this:



This results in this error: "Required request parameter 'accountManagerId' for method parameter type int is not present"

 
Ray Gilbert
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I made this change: and now I get this error:

Required URI template variable 'accountManagerId' for method parameter type int is not present

The code at this point:

Post method:



Accountmanager form:

 
Ray Gilbert
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then I tried changing the saveClientUrl URL to include the accountManagerId value as a path variable:



And I got this error: Required URI template variable 'accountManagerId' for method parameter type int is not present

My code at this point:

Post method:



AccountManager form:

 
Ray Gilbert
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed back to

and then added the the AccountManager form. and I get: Required request parameter 'accountManagerId' for method parameter type int is not present.

My current, non-working code is:

Post method:



AccountManager form:

 
Bartender
Posts: 2466
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not too familiar with JSP or frontend, but I think you may need to save the accountManager object to a ModelMap .
 
Tim Holloway
Saloon Keeper
Posts: 29002
214
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on that last JSP, you need your controller to create an object that's an enumerable collection of AccountManager (List or array) items and store in under Request, Session or Application scope under the name "accountManagers".

For application-wide menus, you can do that once and store in Application Scope. If your SELECT menu varies according to the user or program logic, then Session or Request scope would be the way to go.
 
Ray Gilbert
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so I dropped saving a role with the client for now. I am able to create a client, create an address and associate that address with a client and associate a selected account manager with the client. However, it is saving one client with values and one that is null, as well as one address with values and one that is null. It is assigning the null value address to the client with values and the address with values to the null client.

Here is my Post method:



 
Ray Gilbert
Ranch Hand
Posts: 150
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After 115 iterations of code refactoring, I solved it... Well, I have three of the four functions happening. The forth is simple rinse and repeat at this point. I am creating a client with an address and assigning the client to an account manager.

Her is my final code:

JSP FORM:



POST method:



I would like to thank me for all my help and encouragement. I couldn't have done it without me.
 
Himai Minh
Bartender
Posts: 2466
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ray,
Can  you highlight the changes that you made to make the code work?
 
Are you okay? You look a little big. Maybe this tiny ad will help:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic