0

hi chums I am new to JSF and I got stuck in JSF problem.I want to add dynamically my content in my JSF page.My requirment is that I have one facelets which I have to add in my page after some event.

My page contains

two fields 'user name' and 'answer'
and
two buttons 'login' and 'recover password'

Facelet contains

a field and just a button

Now I want that when I run my this page it should show only Page contents(described above)
and if user press the button 'recover password' then it should show the facelet under the components of page on the same page mean after pressing the button , page could include facelet.
But now when I run my code it show both as I haven't put any logic to add 2nd facelet dynamically.
Here a patch of my code given below

 <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"/>

Now I want when this btnRecover_click click then it include this facelet or run this code

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

Keep in mind that I have to do this with JSF and don't want to do this with JavaScript
Thanks

3
  • You can use the "rendered" attribute to get the desired behavior, no need of javascript as suggested in the answer. To make it work, you should wrap the <ui:include> inside an ui container component like <h:panelGrid>, change the rendered value in server and rerender this container, then your ui will be displayed Commented Aug 3, 2012 at 6:50
  • @LuiggiMendoza but how? can you give me a small example? Commented Aug 3, 2012 at 6:53
  • should I do this " <h:panelGrid rendered="false"> <div class="divPadding"> <ui:include id="passrecov" src="ucPasswordRecovery.xhtml" rendered="false"/> </div> <h:panelGrid> " But it still didn't work :( Commented Aug 3, 2012 at 7:09

1 Answer 1

2

I'll post a sample using with 1 column, remember that you could use another ui container.

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

And your managed 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;
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

i am still getting the issues.first of all in "<h:panelGrid id="passRecovery">" this panel grid "<ui:include id="passrecov" src="ucPasswordRecovery.xhtml" rendered="#{not loginBean.loginEnabled}" is not working but when i put rendered="#{not loginBean.loginEnabled} attribute in "<h:panelGrid id="passRecovery" rendered="#{not loginBean.loginEnabled}>" then it start works.Now after this my issue is that when facelet added in that page then it removes the previous contents of panelgrid(ref: h:panelGrid rendered="#{loginBean.loginEnabled}">).I want that panelgrid still remain present- please help
Please let me know If anything is unclear- I will post it again as an issue and will send you the link of that one
@SSK answer edited, I've added another <h:panelGrid> that will handle the rendered tag attribute.
I run this code exactly but now it is only showing facelet.It means this grid is rendering only "<h:panelGrid id="passRecovery"> <h:panelGrid rendered="#{not loginBean.loginEnabled}"> <ui:include id="passrecov" src="ucPasswordRecovery.xhtml" /> </h:panelGrid> </h:panelGrid>" I am sick from its this behaviour
@SSK yes I've tested the code and it renders the include with no problem. What's your exact problem?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.