Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a component outside of the <f:ajax> component's context

Tags:

jsf

jsf-2

Is it possible to have the JSF update a component that's placed outside the component's context?

Currently the following page is not working:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>Welcome</title>
</h:head>

<h:body>
    <p><h:outputText id="out" value="#{user.greeting}" /></p>

    <h:form id="form1">

        <h:inputText value="#{user.name}" id="user-name" />
        <p><h:inputSecret value="#{user.password}" id="password" /></p>
        <p>
        <h:commandButton value="Login" id="login-button">
            <f:ajax execute="user-name password" render="out" />
        </h:commandButton>
        </p>
    </h:form>
</h:body>

</html>

I know that if I put the #out component inside <h:form> the page will be rendered correctly. But is there a way to place the #out component outside of the form (e.g. where it is right now)?

like image 872
Behrang Avatar asked Jun 24 '10 16:06

Behrang


1 Answers

Solved! It is possible to refer to out as :out. In this way findComponent searches for it starting from the view root. So here's the working solution:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>Welcome</title>
</h:head>

<h:body>
    <p><h:outputText id="out" value="#{user.greeting}" /></p>

    <h:form id="form1">
        <h:inputText value="#{user.name}" id="user-name" />
        <p><h:inputSecret value="#{user.password}" id="password" /></p>
        <p>
        <h:commandButton value="Login" id="login-button">
            <f:ajax execute="user-name password" render=":out" />
        </h:commandButton>
        </p>
    </h:form>
</h:body>

</html>
like image 154
Behrang Avatar answered Nov 11 '22 00:11

Behrang