• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Problem populating Struts2 DAO class from JSP

 
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is part of a user admin application. In this example I want to create a userName associated with a roleName. 'userNam'e and 'roleName' are the only columns in the 'memroles' table in a mySql database. This schema conforms to TOmcat j_security which is used to authenticate users. Here's the problem:

I try to create a userName with an associated roleName using th following jsp:



This produces the following stack:

When I try to manually pass the userName and roleName values as parameters inside the DAO class (RoleDAOImpl) itself, it works fine. So my question is WHY AREN'T THE VALUES BEING PASSED FROM THE JSP TO THE REST OF THE APPLICATION CLASSES?

I'm providing the main excerpts of the application classes. Please let me know if I need to provide any further info. Many thanks!


The Struts action is:





Dao class:


 
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jay,

Check line number 35 of RoleAction


When you wrote new Role(), this simply created a new Role object with the default constructor ( and I am guessing that all the fields were null ) so the userName and roleName value turned out to be null. Instead of a new object, you want the one in the value stack. Try replacing this line with the struts2 model pattern as below

 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the respons Saif. Unfortiunately it produced NullPointerException:

java.lang.NullPointerException
admin.dao.RoleDAOImpl.createRole(RoleDAOImpl.java:40)
admin.main.RoleManager.create(RoleManager.java:34)
admin.main.RoleAction.create(RoleAction.java:51

Is the role object I originally created null or are the userName and roleName fields WITHIN the role object null?

The way you suggested (role = (Role) getModel()) basically populates the role object using the ModelDriven interface right? So if this doesn't work, is my problem because I'm not populating the role instance correctly or is the problem more related to the userName and roleName fields themselves? I notice from my log messages that the role object is being populated even if I use (role = new Role()). This produces the following log message:




The real problem seems to be that userName and roleName fields from the jsp are not being picked up:




 
Saif Asif
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you verify a couple of things for me.

1. The role class has the 2 fields exactly the same as the ones you have mentioned in the JSP ( userName, roleName ) .
2. I forgot to mention an edit in my response, I'll mention it right here.. On line number 19 on RoleAction , replace with


The way you suggested (role = (Role) getModel()) basically populates the role object using the ModelDriven interface right?

Yes that is correct.

I notice from my log messages that the role object is being populated even if I use (role = new Role()). This produces the following log message:


view plainprint?
Note: Text content in the code blocks is automatically word-wrapped
2013-11-12 21:53:20 INFO RoleAction class:52 - the created role object is: admin.main.Role@48e77aae



That is just the hash of the object that you created by the new keyword. You can add logs after creating the object to print the fields userName and roleName to verify their values.

First I would advise you to perform the checks that I have mentioned. Then we will dig deeper into the problem
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked! I followed the your advice as follows:




So can you please explain why this worked? What I really want to know is what's the difference between creating the new instance of the role object as a field declaration as opposed to declaring it within the 'create' method? I would have though Role role = new Role() in the create method would work since this new object gets passed throughout the application. From the above it looks like creating the instance of the role object in the field decalaration is the right way to go. Would be very interested in your comments.

Thank you so much for your help Saif. Really appreciated!!
 
Saif Asif
Ranch Hand
Posts: 440
Hibernate Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all I apologize for the delay in my response, I was caught up in some work and wasn't available for a few days. You might have already done some research on your end, nevertheless I'll still explain the working

You can initiate the new Object at any point in time but the way it gets populated is what matters to us in Struts Model Driver actions. If you initiate at method level ( your action ) as below


There is no significance of this object as it is just telling the JVM to make a new Role object out of thin air ( and you know the rest of the story as how any new object is formed ).

Now, for the ModelDriven action to work, you need to get the object that is residing on the value stack ! This is important. See snippet below



Ofcourse you can also re-write the above snippet as below also


you can also read about it here in the docs

I hope I was clear enough. Do post back for any further queries.
 
What are you doing? You are supposed to be reading this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic