Reputation: 37904
i am trying to render a key,value Map to a template and then show there in this way:
@(twittsIfollow: Map[String, String])
.....
@if(twittsIfollow != null) {
@for((key, value) <- twittsIfollow) {
@key
@value
}
}
it says, it is wrong. is there a scala tag for Map keys values?
here is my method:
public static Map<String, String> alltwitts(List<Long> otherIDs) {
Map<String, String> results=new HashMap<String, String>();
for (Long id: otherIDs) {
Query selected_twitt = JPA.em().createQuery("select u.twitt from Twitt u where " + " u.whose = ?").setParameter(1, id);
String twOwner = User.getOneUser(id);
String twitt = (String) selected_twitt.getSingleResult();
results.put(twOwner, twitt);
}
return results;
}
then i render to template in this place:
Map<String, String> twittsIfollow = Twitt.alltwitts(IDusersIamFollowing);
return ok(microblog.render(twittsIfollow));
now it is saying: [NonUniqueResultException: result returns more than one elements]
thanks
Upvotes: 2
Views: 3914
Reputation: 37904
how i solved:
public static Map<String, List<String>> alltwitts(List<Long> otherIDs) {
Map<String, List<String>> results=new HashMap<String, List<String>>();
for (Long id: otherIDs) {
Query selected_twitt = JPA.em().createQuery("select twitt from Twitt where " + " whose = ? order by id").setParameter(1, id);
String twOwner = User.getOneUser(id);
List<String> twitt = selected_twitt.getResultList();
results.put(twOwner, twitt);
}
System.out.println(results);
return results;
}
then in my Application:
Map<String, List<String>> twittsIfollow = Twitt.alltwitts(IDusersIamFollowing);
return ok(microblog.render(twittsIfollow));
then in my template:
@(twittsIfollow: Map[String, List[String]])
and
@if(twittsIfollow != null) {
@for((key, value) <- twittsIfollow) {
@for(innervalue <- value){
Username: @key
Twitt: @innervalue
}
}
}
just for those who are dumb like me :D and looking for solution like this..
thanks for help,guys..
Upvotes: 3
Reputation: 1841
Simply
@for((key, value) <- twittsIfollow) {
@key
@value
}
BTW, if you are using Scala, twittsIfollow
should never be null. Prefer using Option
.
Upvotes: 9
Reputation: 55798
@doniyor, I'm surpriesed as I showed you the way in other question not so long ago.
By the way I think it will be better to build, pass and iterate twittsIfollow
as a List
of Map
s
then you can use
@if(twittsIfollow != null) {
@for(twitts <- twittsIfollow) {
the value is: @twitts.get("key")
}
}
Upvotes: 1