java - 无法使用 JSF 访问托管 bean 方法

标签 java jsf jsf-2 ejb

您好,我正在尝试访问并调用 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}" />&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>

这是托管 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/

相关文章:

java - 如何优化正则表达式模式?

java - 停止线程并再次开始在黑莓中给出 IllegalThreadStateException

java - Informix、MySQL 和 Oracle blob 包含

jsf - 如何自定义 h :head when using ui:composition template?

javascript - triggerChange() 函数在 JSF 中不起作用?

java - 将 Android 应用程序安装限制在 Android 手机上

html - 使用带有 jsf 重复标记的第 n 个子 css 选择器

jsf - JSF:检查=“id”/> <h:message是否存在的更好方法

jsf-2 - 仅为一个 JSF 页面应用 PrimeFaces 主题

java - 在一个浏览器中使用带有多个选项卡的 JSF