0

I use spring and hibernate in my project everything look fine when insert data into database but when I query and display in jsp the value isn't correct it's look like I print object

Model.User@1c0c66a, Model.User@1228521, Model.User@1526c5f

How to fix this ?? I should encode or I do something wrong when query????

    @Override
public List findByUserName() {
    List list = getHibernateTemplate().find(
            "from User");

    return list;
}

This is method that I use to query data out

List customerList = userdao.findByUserName();

and this statement I use for get return list

${customerList}

and this is EL that I use in jsp

I don't sure what make value like this I think I must forgot something because it can query but it didn't display correctly.

Thank in advance, Mart

1
  • You are printing with the Object-class toString-method, that's why you get class-name and the number is the hashcode of the object. Commented Apr 22, 2011 at 7:51

1 Answer 1

4

use this jstl code instead:

<c:forEach var="customer" items="${customerList}">
  ${customer.name} - ${customer.surname} ...
</c:forEach>

its up to you which fields you want to show (I assumed you have the fields name and surname) and how you want to show them.

To be able to use jstl you need to add this include line in the beginning of your jsp page:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Sign up to request clarification or add additional context in comments.

4 Comments

Can you explain more why I can't use EL ??? (I follow your advice and now it can display correctly :D )
hi, the reason is that jsp doesn't know what to do with your ${customerList} el. Should it display all elements and all its fields? If yes in what order should be the fields displayed? How to format numbers and dates? These are all the questions which jsp can't answer for you. So all it will do is, that it will call the toString() method on your List object. And the result of this could be seen in your original page.
You can use EL. The answer from Matej uses EL. But when you're using ${customerList}, it means "display the customerList object using its toString method. The toString method of java.util.List displays every element it contains (once again using their toString method), separated with commas. And since you diodn't override toString in your Model.User class, it uses the default toString method of java.lang.Object, wich displays the objetc's class name followed by '@' and its hashCode.
Thank for your advice, Matej Tymes and JB Nizet. It very helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.