2

Hi I'm trying to access and call a method inside a managed bean in my JSF page. Here is the relevant part of the JSF page:

<ui:composition xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
            xmlns:h="http://xmlns.jcp.org/jsf/html"
            xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
            xmlns:c="http://java.sun.com/jsp/jstl/core"
            xmlns="http://www.w3.org/1999/xhtml"
            template="./template.xhtml">

<ui:define name="right">
    <c:forEach items="#{tweetManager.getTweets}" var="item">
        <h:link value="#{item.username}" />&nbsp;
        Likes: &nbsp;&nbsp;<h:outputText value="#{item.likes}" />&nbsp;&nbsp;&nbsp;
        <h:link id="" value="like" />&nbsp;<br />
        <textarea>
            <h:outputText value="#{item.text}" />
        </textarea>
    </c:forEach>
</ui:define>

Here is the managed bean.

@ManagedBean
@RequestScoped
public class TweetManager {
private Tweet TweetEntity;
private List<Tweet> Tweets;

@EJB
private TweetService TweetService;

@PostConstruct
public void init(){
    TweetEntity = new Tweet();
}

public void setTweet(Tweet tweetEntity){
    this.TweetEntity = tweetEntity;
}

public Tweet getTweet(){
    return this.TweetEntity;
}

public void Save(){
    TweetService.create(TweetEntity);
}

public List<Tweet> getTweets(){
    Query query = TweetService.getEntityManager().createNativeQuery("SELECT * FROM tweet");
    Tweets = query.getResultList();
    return Tweets;
   }
}

I'm getting an error saying: ... .TweetManager' does not have the property 'getTweets'.

user2455558
  • 127
  • 2
  • 9
  • 1
    Which version of ExpressionLanguage your using? Try: – Michel May 14 '14 at 07:35
  • Your code shows two `getTweets()` (I assume it was a problem redacting the question). Anyway, as you do not use generics, once this issue is resolved you will get an error with `item.username` (since, as far as the compiler knows, `item` is an `Object`). Make `getTweets()` return a `List` instead of `List`. – SJuan76 May 14 '14 at 07:37
  • Should have revised my code once more before posting. Yes it worked with getWeets() I've added all the versions of xmlns I've imported. – user2455558 May 14 '14 at 07:45

1 Answers1

3

getTweet() is not a property, it is the accessor (or "getter") of the property.

The name of the property is tweets (without the get, first letter to lowercase). So:

<c:forEach items="#{tweetManager.tweets}" var="item">

Remember that boolean properties have "getters" like "is" (v.g. isRich())

And keep in mind my comment about using generics.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • 1
    Worthy to note: the getter for a Boolean type is still expected to be getRich(), not isRich(). – Gimby May 14 '14 at 07:50
  • 1
    Gimby is right (the difference is that `Boolean` is an object, not a primitive). Also I revised the docs and seems that `getRich` is also valid for a `boolean` property (more about that: http://stackoverflow.com/questions/799280/valid-java-bean-names-for-booleans) – SJuan76 May 14 '14 at 07:55
  • Indeed. And then create the following fun scenario: have a class hierarchy and in one layer you have a getXXX() and in another layer you have an isXXX(). Which one is JSF going to choose? Always the wrong one, in my experience :) – Gimby May 14 '14 at 07:58