2
votes

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'.

1
Which version of ExpressionLanguage your using? Try:<c:forEach items="#{tweetManager.getTweets()}" var="item"> - Michel
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<Tweet> instead of List. - SJuan76
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

1 Answers

3
votes

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.