Accessing multiple entities with one jsp
posted 2 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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:
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:
posted 2 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
posted 2 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Yeah, ignore that jsp code, that was a last ditch attempt to get chatgpt to help me. Not recommended. Lol
52 y.o. and unable to do any of my old skillsets anylonger and struggling against time and starvation to learn to code.
posted 2 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 2 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
So, I tried this:
This results in this error: "Required request parameter 'accountManagerId' for method parameter type int is not present"
This results in this error: "Required request parameter 'accountManagerId' for method parameter type int is not present"
posted 2 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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:
Required URI template variable 'accountManagerId' for method parameter type int is not present
The code at this point:
Post method:
Accountmanager form:
posted 2 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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:
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:
52 y.o. and unable to do any of my old skillsets anylonger and struggling against time and starvation to learn to code.
posted 2 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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:
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:
posted 2 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I am not too familiar with JSP or frontend, but I think you may need to save the accountManager object to a ModelMap .
posted 2 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
Experience keeps a dear School, but Fools will learn in no other.
---
Benjamin Franklin - Postal official and Weather observer
posted 2 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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:
Here is my Post method:
52 y.o. and unable to do any of my old skillsets anylonger and struggling against time and starvation to learn to code.
posted 2 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
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.
52 y.o. and unable to do any of my old skillsets anylonger and struggling against time and starvation to learn to code.
Himai Minh
Bartender
Posts: 2466
13
posted 2 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Ray,
Can you highlight the changes that you made to make the code work?
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 |










