• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Unable to retrive priomary key values in Struts2 CRUD application

 
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm developing a Struts2 application which is simply supposed to display a list of users from a mySQL DB after a successful j_security login using Tomcat Database Realm. The application succesfully returns all user records in the DB after user authentication EXCEPT the primary key values corresponding to each user are ZERO instead of the values in the database. I'm not sure if the primary key (MemID) is being incorrectly evaulated by ONGL in the jsp part or if I'm making mistakes in the action class or other classes in this application. I did try modifying the jsp to use the %{user.MemID} syntax. I also tried using JSTL tags but none of this solved the problem.

I would greatly appreciate some guidance. Thank you! The main files of my app are:







list.jsp
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
300 lines of code, huh? Can you try narrowing down the problem a little?
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Joe. I was trying to be too detailed. My first guess is the problem is in the Struts action class. My second guess is it's the ONGL tags in the jsp. I'm not able to edit the original post anymore so I pasted these files below for convenience.

When this class is used by the jsp it renders the user record except the primary key (memberid) which is column "id" in the database. I was hoping memberid would be retrieved from the action class, since I've declared this as a field of type int. When I use the action class below, the jsp renders a zero (0) value next to each user record, so I get firstName, lastName, 0 (instead of the corresponding primary key of each record).

So my question is what is the 'correct' way to retrieve primary keys in Struts2? If the action class is the culprit, what modifications should I make to this class in order to retrieve the primary keys correctly instead of a bunch of zeros? If the problem is in how the primary key int is being processed in the jsp, how should I modiy the ONGL? Does this help narrow down the problem? Thanks!




 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does your action have a get/setUser and get/setMemID? Since you aren't setting those values, I'll bet Struts is picking up the memID from there.
One nitpick: the property tag value should be "memID" (this is dictated by the JavaBean specification. A property "memID" is assumed to have a getter of getMemID and a setter of setMemID. I don't know if the value actually makes a difference in this case).
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Appreciate the explanation but I'm still not clear. I can see the logic of omitting the get/setUser and get/setMemID from the action class. I also changed the property value in the jsp to 'memID'. Now the action class looks like



Now the primary key column in simply blank/null (not zero). The user and MemID get/set are already defined my User bean. After omiting the getter/setters in the new action class above, do I now need a method to retrieve or set the MemID in the action class (ie: by retrieving from the User bean)? Is it simply a case of omitting the getter /setters as above or do I need an alternative method to retrieve the MemID? Thank you!
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Tai wrote:. After omiting the getter/setters in the new action class above, do I now need a method to retrieve or set the MemID in the action class (ie: by retrieving from the User bean)? Is it simply a case of omitting the getter /setters as above or do I need an alternative method to retrieve the MemID? Thank you!



No. If you look at the iterator tag, you'll see that it is placing the current User on the Value Stack, so any reference to memID will be resolved to it.
At this point, you should try logging the Users you are getting back from the DAO to make sure it contains what you think it does.
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for my late response. I had trouble accessing this site for a couple of days. Just to make sure I understand, does this mean I should log from the DAO file or from the jsp itself?
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the action is the correct place to dump the list of users.
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so I included the following logging in the action class:



The output was:

***The memID set in the action class is: 0
***The memID set in the action class is: 0
***The memID set in the action class is: 0
***The memID set in the action class is: 0

There are 4 records in the DB so it looks like the memID isn't being set from the the query.

 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, why does your action have a get/setUser and get/setMemID?
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My mistake. I understand that having getter/setter methods basically 'resets' the PK in the action class, but if I omit these methods is the PK (memID) set by the user bean or the DAO class which queries the db? The following is the action class without the getter / setter and with some logging on the user list:



The output:

the list method from the action class returns the following users: [admin.beans.User@51570f71, admin.beans.User@2ab6ed6f, admin.beans.User@4f9e2851, admin.beans.User@186906ad]
the list method from the action class returns the following PK numbers:java.util.ArrayList$ListItr@17355460

The user list is being populated but I'm not sure how to list the memID from the user list using the above logging

 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Tai wrote:

The output:

the list method from the action class returns the following users: [admin.beans.User@51570f71, admin.beans.User@2ab6ed6f, admin.beans.User@4f9e2851, admin.beans.User@186906ad]
the list method from the action class returns the following PK numbers:java.util.ArrayList$ListItr@17355460



public String toString()
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())


Java Documentation for Object.toString
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that. Still not sure how to use toString to extract just the PK values from the list.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())



Does this sugest that I have to override toString and include the hexadecmia part?


Here's what I tried which is a simple implementation of toString:



Output:

the list method from the action class returns the following users: [admin.beans.User@42f79c38, admin.beans.User@e3142e1, admin.beans.User@441dc90e, admin.beans.User@7f7ad911]
the list method from the action class returns the following PK numbers:admin.beans.User@42f79c38
the list method from the action class returns the following PK numbers:admin.beans.User@e3142e1
the list method from the action class returns the following PK numbers:admin.beans.User@441dc90e
the list method from the action class returns the following PK numbers:admin.beans.User@7f7ad911
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's try that again:

public String toString()
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())



Alternatively, you could declare "i" in your for loop to be a User and invoke getters on it.
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For now I will use the for loop statement while I get a better idea of how to override toString from the java documentation.



the list method from the action class returns the following users: [admin.beans.User@43b077a, admin.beans.User@3f67dcc1, admin.beans.User@3bce236c, admin.beans.User@5a77a620]
the list method from the action class memID is: :0 lastNAme Smith
the list method from the action class memID is: :0 lastNAme Mills
the list method from the action class memID is: :0 lastNAme Brekelbaum
the list method from the action class memID is: :0 lastNAme Simmons

I think iit's safe to say the memID is not being correctly set from the jsp. Right? In this case, could the problem be related to the DAO class that executes the actual query?
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Tai wrote:
I think iit's safe to say the memID is not being correctly set from the jsp. Right? In this case, could the problem be related to the DAO class that executes the actual query?



We're looking at the values before the JSP is rendered. Yes, the problem could be DAO related.
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

We're looking at the values before the JSP is rendered. Yes, the problem could be DAO related



If the memID value is zero BEFORE the jsp is rendered, I'm assuming it's likely to be zero after jsp is rendered since the jsp uses the action class as intermediate layer before the DAO. How can I log the jsp after rendering? Do I need to put the logging message within the jsp itself to see the rendered values for memID?

Looking at the DAO class (below) I can't see what problem would cause ONLY the primary key not to be correctly rendered.




I actually have a working example which uses a more complex DAO structure (including a DAO factory). This successfully lists primary key and the full record, but only accepts a certain value in the jsp ONGL (which is not related to the primary key value in the action class and the bean). Should I post excerpts of this other example here or separately? Should I just continue trying to solve this simpler example to understand the underlying Struts technology?
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well I restarted my IDE (Netbeans) and the output was successful

the list method from the action class returns the following users: [admin.beans.User@1d3ff748, admin.beans.User@5283a6f8, admin.beans.User@41470e1a, admin.beans.User@1feeee71]
the list method from the action class memID is: :11 lastNAme Smith
the list method from the action class memID is: :22 lastNAme Mills
the list method from the action class memID is: :24 lastNAme Brekelbaum
the list method from the action class memID is: :29 lastNAme Simmons

The jsp still renders blank valuues for PK (memID) and this is the case no matter which value i use for the iterator property in the jsp. At least now I can see that the memID is being read before the jsp is rendered.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the problem is on this line in your JSP:
To tie up with the methods setMemId and getMemId (note the case), this line should be:

Also, in your logging, you are actually outputting the first name but the log entry states incorrectly that it is the last name.
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes! That totally fixed the problem

I'd like to understand what went wrong. So the ONGL value in jsp has to match the exact case in the getter/setter method name of the object bean? It has tie up with the method name in the bean class because i took out the MemId getter/setter methods from the action class. So the bean class is the only place where the MemId getter/setter methods exist. For example, in my User bean class I have:



so even thoough the returned and set values are actually MemID (note case), the value which invokes the correct values from the jsp is 'MemId', which ties up with setMemId and get MemId?

Thanks a bunch for your help and patience!!
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jay

The way to look at it is this. Whatever property value you have, say XXX, the setter and getter must be called setXXX and getXXX with the first character of the property name capitalised.

Example 1
Property name: abc
Method names: getAbc, setAbc

Example 2
Property name: aBC
Method names: getABC, setABC

Example 3
Property name: abC
Method names: getAbC, setAbC

Example 4
Property name: ABC
Method names: getABC, setABC
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James,

Thanks for the explanation and that makes thing clearer. So in the context of this application, it actually turns out that the jsp accepts 'memId' or 'MemId' in the ONGL property field.

This tells me the jsp doesn't care if the first letter is capitalized or not (because, referring to Jame's example, the ONGL assumes the first letter is always capitalized). Correct?

If this is right, it would mean that the rest of the property name has to match the method name case.

If the method name is 'getMemId', the jsp property will accept 'memId' or 'MemId', BUT 'memID' or 'MemID' would be incorrect

Do i have the the right idea?
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Tai wrote:If the method name is 'getMemId', the jsp property will accept 'memId' or 'MemId'


Not sure about MemId. Why don't you try it out?
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I did try it and it accepts 'memId' and 'MemId'. I'm just trying to understand the concept.

Looks like the jsp property wants the exact case of the getter/setter method names. It doesn't worry about the first letter because it assumes that has to be capitalized so ignores the case?
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like you have grasped it Jay.
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James, Thanks again for making this clear!
 
reply
    Bookmark Topic Watch Topic
  • New Topic