开发者

Print all bean properties in EL without need to override toString() every time

I have a list/map of beans in EL, or at least I assume it is and I'm fed up with spending hours going through trying to work out which sections belong to each other. So I print it plain like ${example}.

football.beans.FootballFixture@72bec69[match=f开发者_C百科ootball.domain.FootballMatch@773aa1d5[id=360496,competition=PREMIERSHIP,seasonId=2011,groupName=,roundType=,roundNumber=,matchPeriod=FULL_TIME,matchDay=3,venueId=33,venue=White Hart Lane,venueCity=London,homeTeamId=t6,awayTeamId=t43,homeScore=1,awayScore=5,scorers=[football.domain.Score@3d5bed54[567825,AWAY,2011-08-28 14:05:05.0,34,GOAL,Dzeko,,FIRST_HALF

Currently I am overriding toString() on my beans every time. It'd be great to have something similar to PHP's print_r in JSP. Anybody know how to stop my eternal headache caused by this problem.


Import the Jackson JSON library and do something like this (assuming "myList" is the list/map you already have defined):

 ObjectMapper mapper = new ObjectMapper();
 System.out.println( mapper.defaultPrettyPrintingWriter().writeValueAsString(myList) );

This would print out your complete list object as a string to standard out.

Hope this helps.


You can use Pojomatic lib.

If you use maven, add dependency to pom.xml.

<dependency>
    <groupId>org.pojomatic</groupId>
    <artifactId>pojomatic</artifactId>
    <version>1.0</version>
</dependency>

Then add @AutoProperty annotation to class you want to read like PostVo.java.

And, override toString method like this:

@Override
 public String toString() {
     return Pojomatic.toString (this);
}

You can override hashCode() and equals(Object obj).

@Override
 public boolean equals(Object obj) {
     return Pojomatic.equals (this, obj);
}

@Override
 public int hashCode() {
     return Pojomatic.hashCode (this);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜