使用JSF添加动态内容

时间:2012-08-03 05:36:38

标签: java java-ee jsf-2 facelets

hi chums我是JSF的新手,我遇到了JSF问题。我想在我的JSF页面中动态添加我的内容。我的要求是我有一个facelets,我必须在一些事件之后添加到我的页面中。

我的页面包含

两个字段'用户名'和'回答'

两个按钮'登录'和'恢复密码'

Facelet包含

一个字段,只是一个按钮

现在我希望当我运行这个页面时,它应该只显示页面内容(如上所述)
如果用户按下按钮'recover password',那么它应该在同一页面上的页面组件下显示小面板意味着按下按钮后,页面可能包括小面板。
但是现在当我运行我的代码时,它显示了两个,因为我没有动态添加第二个facelet的任何逻辑。
这里有我的代码补丁

 <h:inputText id="txtLogin" styleClass="textEntry" required="true" requiredMessage="Username is required."> 
                    </h:inputText>
<h:inputText id="answer" styleClass="textEntry" autocomplete="off" required="true" requiredMessage="Answer is required."></h:inputText>
                                    <h:commandButton id="btnSave" value="Save" styleClass="formbutton" onclick="btnSave_Click" />
                    <h:commandButton id="btnRecover" value="Recover" styleClass="formbutton" onclick="btnRecover_Click"/>

现在我希望当btnRecover_click点击它然后它包含这个facelet或运行此代码

<div class="divPadding">
            <ui:include id="passrecov" src="ucPasswordRecovery.xhtml" rendered="false"/>
        </div>

请记住,我必须使用JSF执行此操作,并且不希望使用JavaScript执行此操作
感谢

1 个答案:

答案 0 :(得分:2)

我会使用1列发布样本,记住你可以使用另一个ui容器。

<h:form>
    <h:panelGrid id="login">
        <h:panelGrid>
            <h:inputText id="txtLogin" styleClass="textEntry" required="true"
                requiredMessage="Username is required."> 
            </h:inputText>
            <h:inputText id="answer" styleClass="textEntry" autocomplete="off"
                required="true" requiredMessage="Answer is required.">
            </h:inputText>
            <h:commandButton id="btnSave" value="Save" styleClass="formbutton"
                onclick="btnSave_Click" />
            <h:commandButton id="btnRecover" value="Recover" styleClass="formbutton"
                action="#{loginBean.showPassRecovery}" />
        </h:panelGrid>
    </h:panelGrid>

    <h:panelGrid id="passRecovery">
        <h:panelGrid rendered="#{not loginBean.loginEnabled}">
        <ui:include id="passrecov" src="ucPasswordRecovery.xhtml" />
        </h:panelGrid>
    </h:panelGrid>
</h:form>

你的托管bean:

@ManagedBean
@ViewScoped
public class LoginBean {
    private boolean loginEnabled;

    public LoginBean() {
        loginEnabled = true;
    }

    //getters and setters...

    //this method will return void, so the page will be refreshed with the new
    //loginEnabled value. this time the components inside "login" panelGrid won't be rendered
    //but "passRecovery" panelGrid components will.
    public void showPassRecovery() {
        loginEnabled = false;
    }
}