JSF使用commandbutton激活呈现的属性

时间:2011-05-04 12:06:00

标签: jsf

我所拥有的是jsf页面,它在一个表中显示用户,其中一列是commandbutton,它隐藏了该表并显示了另一个表,其中包含该用户拥有的所有文章。这些事情都可以。 articles表再次包含命令按钮的列,该按钮必须隐藏该表并显示文章内容,但它显示用户表。在我的bean中我有3个属性

    private Boolean usersViewActive;
    private Boolean articlesViewActive;
    private Boolean articleContentViweActive;

以及激活其中一个并停用其他人的方法

    public void activateUsersView() {
        this.usersViewActive = true;
        this.articlesViewActive = false;
        this.articleContentViweActive=false;
    }

    public void activateArticleView() {
        this.usersViewActive = false;
        this.articlesViewActive = true;
        this.articleContentViweActive = false;
    }

    public void activateArticleContentView() {
        this.articleContentViweActive = true;
        this.usersViewActive = false;
        this.articlesViewActive = false;
    }

我的豆子

@ManagedBean
@RequestScoped
public class DataBean {
    private List<UserAcc> users;
    private List<Article> articles;
    private List<ArticleComment> articleComments;

    private Article currentArticle;

    private Boolean usersViewActive;
    private Boolean articlesViewActive;
    private Boolean articleContentViweActive;

    public DataBean() {
        DataBaseUtil dbu = new DataBaseUtil();
        users = dbu.loadUsers();
        this.usersViewActive = true;
    }

    public void activateUsersView() {
        this.usersViewActive = true;
        this.articlesViewActive = false;
        this.articleContentViweActive=false;
    }

    public void activateArticleView() {
        this.usersViewActive = false;
        this.articlesViewActive = true;
        this.articleContentViweActive = false;
    }

    public void activateArticleContentView() {
        this.articleContentViweActive = true;
        this.usersViewActive = false;
        this.articlesViewActive = false;
    }

    public void loadArticles(ActionEvent e) {
        DataBaseUtil dbu = new DataBaseUtil();
        int userId = (Integer) e.getComponent().getAttributes().get("userId");
        articles = dbu.loadUserArticles(userId);
    }

    public void loadComments(ActionEvent e) {
        DataBaseUtil dbu = new DataBaseUtil();
        int articleId = (Integer) e.getComponent().getAttributes()
                .get("articleId");
        articleComments = dbu.loadArticleComments(articleId);
    }

    public void loadCurrentArticle(ActionEvent e) {
        DataBaseUtil dbu = new DataBaseUtil();
        int articleId = (Integer) e.getComponent().getAttributes()
                .get("articleId");
        this.currentArticle = dbu.loadArticleById(articleId);
    }

  Getters and Setters...
}

我的页面:

<h:body>
    <h:panelGroup rendered="#{loginBean.authorised}">
        User: <h:outputText value="#{loginBean.name}"></h:outputText>
        <a href="new_article.jsf">Add article</a>
    </h:panelGroup>
    <h:panelGroup rendered="#{not loginBean.authorised}">
        <h:outputText>You are not authorised to be here!</h:outputText>
        <a href="index.jsf">Login or Registrate</a>
    </h:panelGroup>

    <h:form>
        <h:panelGroup rendered="#{loginBean.authorised}">

            <!--                    All users -->
            <h:panelGroup rendered="#{dataBean.usersViewActive}">
                <rich:dataTable value="#{dataBean.users}" var="user" rows="10">
                    <f:facet name="header">Users</f:facet>
                    <rich:column>
                        <f:facet name="header">User Id</f:facet>
                        <h:outputText value="#{user.id}"></h:outputText>
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">User Name</f:facet>
                        <h:outputText value="#{user.name}"></h:outputText>
                    </rich:column>
                    <rich:column>
                        <h:commandButton value="Show articles"
                            action="#{dataBean.activateArticleView}"
                            actionListener="#{dataBean.loadArticles}">
                            <f:attribute name="userId" value="#{user.id}"></f:attribute>
                        </h:commandButton>
                    </rich:column>
                    <f:facet name="footer">
                        <rich:dataScroller></rich:dataScroller>
                    </f:facet>
                </rich:dataTable>
            </h:panelGroup>

            <!--                    All articles -->
            <h:panelGroup rendered="#{dataBean.articlesViewActive}">
                <h:commandButton value="Back to users"
                    action="#{dataBean.activateUsersView}"></h:commandButton> 
                <rich:dataTable value="#{dataBean.articles}" var="article" rows="10">
                    <f:facet name="header">Articles</f:facet>
                    <rich:column>
                        <f:facet name="header">Article Id</f:facet>
                        <h:outputText value="#{article.id}"></h:outputText>
                    </rich:column>
                    <rich:column>
                        <f:facet name="header">Article Title</f:facet>
                        <h:outputText value="#{article.title}"></h:outputText>
                    </rich:column>
                    <rich:column>
                    <!-- Here is the problem  -->
                    <!-- This button renders all users instead article content -->
                        <h:commandButton value="Show article content and comments" 
                        action="#{dataBean.activateArticleContentView}"
                        actionListener="#{dataBean.loadCurrentArticle}">
                            <f:attribute name="articleId" value="#{article.id}"></f:attribute>
                        </h:commandButton>
                    </rich:column>
                    <f:facet name="footer">
                        <rich:dataScroller></rich:dataScroller>
                    </f:facet>
                </rich:dataTable>
            </h:panelGroup>

                <!-- Article content -->
            <h:panelGroup rendered="#{dataBean.articleContentViweActive}">
                <h:outputText value="#{dataBean.currentArticle.title}"></h:outputText>
                <h:outputText value="#{dataBean.currentArticle.text}"></h:outputText>
            </h:panelGroup>
        </h:panelGroup>
    </h:form>

</h:body>

1 个答案:

答案 0 :(得分:3)

您的bean是请求作用域,并且您没有为后续请求维护/保留rendered属性后面的值。当在表单提交时构造新的新请求范围bean时,所有这些值都默认为false。在表单提交期间rendered属性评估false时,不会调用commandbutton的操作。 JSF将在处理表单提交之前再次测试它。

将bean放在视图范围内。

@ManagedBean
@ViewScoped
public class DataBean implements Serializable {
    // ...
}

这样,只要您通过在操作方法中返回voidnull来与同一视图进行交互,bean就会存在。