简体   繁体   中英

Can't access managed bean method using JSF

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

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" (vg isRich() )

And keep in mind my comment about using generics.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM