您好,我正在尝试访问并调用 JSF 页面中托管 Bean 内的方法。以下是 JSF 页面的相关部分:
<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}" />
Likes: <h:outputText value="#{item.likes}" />
<h:link id="" value="like" /> <br />
<textarea>
<h:outputText value="#{item.text}" />
</textarea>
</c:forEach>
</ui:define>
这是托管 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;
}
}
我收到一条错误消息:....TweetManager' 没有属性“getTweets”。
最佳答案
getTweet()
不是一个属性,它是该属性的访问器(或“getter”)。
该属性的名称是 tweets
(不带 get
,首字母小写)。所以:
<c:forEach items="#{tweetManager.tweets}" var="item">
请记住,boolean
属性具有“getter”,例如“is”(例如 isRich()
)
请记住我关于使用泛型的评论。
关于java - 无法使用 JSF 访问托管 bean 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23648241/