I am trying to launch a pop-up window using JSF, where the pop-up window
and base window share the same managed bean.  There is an example of how
to do this in Sun's Core JavaServer Faces book (Chapter 12) that defines
two JSF pages that share a common managed bean:

1. index2.jsp
2. popup2.jsp

When one clicks on a command button in index2.jsp, it executes
JavaScript to launch a popup window, and then it submits a form that
puts popup2.jsp into the popup window.  However, the code doesn't seem
to work.  I list the code at the end of this e-mail.

There are two problems with the code:

1. From doing a "view source" on the generated HTML, I can see that HTML
anchor tag rendered by <h:commandLink> does not specify a name for the
control; it just specifies an id for the control.  Therefore, the
JavaScript in index2.jsp does not recognize hidden["hidden:go"] as a
valid object, since there is no control with the name "hidden:go"
(there's only a control with the *id* of "hidden:go").  I can bypass
this problem by referring to this control as hidden[1] instead of
hidden["hidden:go"], although that's kind of klugey.  Still, that
eliminates that problem.  The next problem is the real kicker.

2. When the popup gets displayed, it is not populated with popup2.jsp.
Rather, it is populated with another copy of index2.jsp.  Just in case
there were errors in index2.jsp, I tried using a blank JSP as the popup
JSP, but, again, the popup was populated with index2.jsp rather than the
blank JSP.

Any ideas about why this doesn't work?  I tried it using two different
JSF reference implementations (including MyFaces), but I get the same
errors.



index2.jsp:

<html>
   <%@ taglib uri="http://java.sun.com/jsf/core"; prefix="f" %>
   <%@ taglib uri="http://java.sun.com/jsf/html"; prefix="h" %>

   <f:view>
      <head>                  
         <script language="JavaScript1.1">
            function doPopup(source) {
               country = source.form[source.form.id + ":country"];
               for (var i = 0; i < country.length; i++) {
                  if (country[i].checked) {
                     popup = window.open("",
                        "popup", 
                        "height=300,width=200,toolbar=no,menubar=no,"
                        + "scrollbars=yes");               
                     popup.openerFormId = source.form.id;
                     popup.focus();
                     var hidden = document.forms.hidden;
                     hidden["hidden:go"].value = "x"; // any value will
do
                     hidden["hidden:country"].value = country[i].value;
                     hidden.submit();          
                  }
               }
            }               
         </script>
         <title>A Simple Java Server Faces Application</title>
      </head>
      <body>
         <h:form>
            <table>
               <tr>
                  <td>Country:</td>
                  <td>
                     <h:selectOneRadio id="country"
value="#{bb.country}">
                        <f:selectItem itemLabel="USA"  itemValue="USA"/>
                        <f:selectItem itemLabel="Canada"
itemValue="Canada"/>
                     </h:selectOneRadio>
                  </td>
               </tr>             
               <tr>
                  <td>State/Province:</td>
                  <td>
                     <h:inputText id="state" value="#{bb.state}"/>
                  </td>
                  <td>
                     <h:commandButton value="..."
                        onclick="doPopup(this); return false;"/>
                  </td>
               </tr>
            </table>
            <p>
               <h:commandButton value="Next" action="next"/>
            </p>
         </h:form>

         <%-- This hidden form sends a request to a popup window. --%>
         <h:form id="hidden" target="popup">
            <h:inputHidden id="country" value="#{bb.country}"/>
            <h:commandLink id="go" action="showStates" value="">
               <f:verbatim/>
            </h:commandLink>
         </h:form>
      </body>
   </f:view>
</html>


popup2.jsp:

<html>
   <%@ taglib uri="http://java.sun.com/jsf/core"; prefix="f" %>
   <%@ taglib uri="http://java.sun.com/jsf/html"; prefix="h" %>

   <f:view>
      <head>                  
         <script language="JavaScript1.1">
            function doSave(value) {
               var formId = window.openerFormId;
               opener.document.forms[formId][formId + ":state"].value =
value;
               window.close();
            }               
         </script>
         <title>Select a state/province</title>
      </head>
      <body>
         <h:form>
            <h:dataTable value="#{bb.statesForCountry}" var="state">
               <h:column>
                  <h:outputLink value="#" 
                     onclick="doSave('#{state}');">
                     <h:outputText value="#{state}" />
                  </h:outputLink>
               </h:column>
            </h:dataTable>
         </h:form>
      </body>
   </f:view>
</html>



faces-config.xml:

<faces-config>
   <navigation-rule>
      <navigation-case>
         <from-outcome>showStates</from-outcome>
         <to-view-id>/popup2.jsp</to-view-id>
      </navigation-case>
   </navigation-rule>

   <managed-bean> 
      <managed-bean-name>bb</managed-bean-name>
      <managed-bean-class>com.corejsf.BackingBean</managed-bean-class> 
      <managed-bean-scope>session</managed-bean-scope> 
   </managed-bean>
</faces-config>


- Brendan

Reply via email to