2011-08-20 95 views
4

我有一个带有几个字段的JSF页面。我跟着BalusC的这个tutorial,一切都很好。然后我添加了RichFaces支持,它工作正常。我可以看到弹出窗口等工作。如何从richfaces弹出窗口刷新父级jsf页面

现在我所要做的是,一旦一个人已被注册,我要显示在弹出的名字(这也是很好我可以看到,在弹出)。然后,我想能够在弹出窗口中更改该名称,并将其反映在父项中,但我不知道如何做到这一点。请看一看。

XHTML页面

<!DOCTYPE html> 
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:a4j="http://richfaces.org/a4j" 
    xmlns:rich="http://richfaces.org/rich"> 
<h:head> 
    <title>Registration</title> 
</h:head> 
<h:body> 
    <h:form> 
     <h:panelGrid columns="3"> 
      <h:outputLabel for="username">Username</h:outputLabel> 
      <h:inputText id="username" value="#{register.user.username}" 
       required="true"> 
       <f:ajax event="blur" render="usernameMessage" /> 
      </h:inputText> 
      <h:message id="usernameMessage" for="username" /> 

      <h:outputLabel for="password">Password</h:outputLabel> 
      <h:inputSecret id="password" value="#{register.user.password}" 
       required="true" redisplay="true"> 
       <f:ajax event="blur" render="passwordMessage" /> 
      </h:inputSecret> 
      <h:message id="passwordMessage" for="password" /> 

      <h:outputLabel for="email">Email</h:outputLabel> 
      <h:inputText id="email" value="#{register.user.email}" 
       required="true"> 
       <f:ajax event="blur" render="emailMessage" /> 
      </h:inputText> 
      <h:message id="emailMessage" for="email" /> 

      <h:outputLabel for="birthdate">Birthdate (yyyy-MM-dd)</h:outputLabel> 
      <h:inputText id="birthdate" value="#{register.user.birthdate}"> 
       <f:convertDateTime pattern="yyyy-MM-dd" /> 
       <f:ajax event="blur" render="birthdateMessage" /> 
      </h:inputText> 
      <h:message id="birthdateMessage" for="birthdate" /> 

      <h:panelGroup /> 
      <h:commandButton value="Register" action="#{register.submit}"> 
       <f:ajax execute="@form" render="@form" /> 
      </h:commandButton> 
      <h:commandButton value="Edit Name in Popup"> 
       <rich:componentControl target="popup" operation="show" /> 
      </h:commandButton> 
      <h:messages globalOnly="true" layout="table" /> 
     </h:panelGrid> 
     <rich:popupPanel id="popup" modal="true" resizeable="true" 
      onmaskclick="#{rich:component('popup')}.hide()"> 
      <f:facet name="header"> 
       <h:outputText value="Simple popup panel" /> 
      </f:facet> 
      <f:facet name="controls"> 
       <h:outputLink value="#" 
        onclick="#{rich:component('popup')}.hide(); return false;"> 
        X 
       </h:outputLink> 
      </f:facet> 

      <h:panelGrid columns="3"> 
       <h:outputLabel for="username2">Username</h:outputLabel> 
       <h:inputText id="username2" value="#{register.user.username}" 
        required="true"> 
       </h:inputText> 
       <h:commandButton value="Register" action="#{register.update}"> 

       </h:commandButton> 
      </h:panelGrid> 

     </rich:popupPanel> 
    </h:form> 
</h:body> 
</html> 

Java类

package com.example; 

import java.io.Serializable; 

import javax.faces.application.FacesMessage; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 
import javax.faces.context.FacesContext; 

@ManagedBean 
@ViewScoped 
public class Register implements Serializable { 

    private static final long serialVersionUID = 1L; 

    private User user; 

    public Register() { 
     user = new User(); 
    } 

    public void submit() { 
     FacesMessage message = new FacesMessage("Registration succesful!"); 
     FacesContext.getCurrentInstance().addMessage(null, message); 
    } 

    public void update() { 
     FacesMessage message = new FacesMessage("Update succesful!"); 
     FacesContext.getCurrentInstance().addMessage(null, message); 
    } 

    public User getUser() { 
     return user; 
    } 

} 

用户类别

package com.example; 

    import java.io.Serializable; 
    import java.util.Date; 

    public class User implements Serializable { 

     private static final long serialVersionUID = 1L; 

     private Long id; 
     private String username; 
     private String password; 
     private String email; 
     private Date birthdate; 
     public Long getId() { 
      return id; 
     } 
     public void setId(Long id) { 
      this.id = id; 
     } 
     public String getUsername() { 
      return username; 
     } 
     public void setUsername(String username) { 
      this.username = username; 
     } 
     public String getPassword() { 
      return password; 
     } 
     public void setPassword(String password) { 
      this.password = password; 
     } 
     public String getEmail() { 
      return email; 
     } 
     public void setEmail(String email) { 
      this.email = email; 
     } 
     public Date getBirthdate() { 
      return birthdate; 
     } 
     public void setBirthdate(Date birthdate) { 
      this.birthdate = birthdate; 
     } 

    } 

阿尔扬Tijms'的建议后更新位

<h:form id="form2"> 
    <rich:popupPanel id="popup" modal="true" resizeable="true" onmaskclick="#{rich:component('popup')}.hide()"> 
     <f:facet name="header"> 
      <h:outputText value="Simple popup panel" /> 
     </f:facet> 
     <f:facet name="controls"> 
      <h:outputLink value="#" onclick="#{rich:component('popup')}.hide(); return false;"> 
       X 
      </h:outputLink> 
     </f:facet> 

     <h:panelGrid columns="3"> 
      <h:outputLabel for="username2">Username</h:outputLabel> 
      <h:inputText id="username2" value="#{register.user.username}" required="true"></h:inputText> 
      <h:commandButton value="Register" action="#{register.update}"> 
       <f:ajax execute="@form" render=":form" /> 
      </h:commandButton> 
     </h:panelGrid> 

    </rich:popupPanel> 
</h:form> 

回答

4

只需推动rich:popupPanel出来的主要形式,并给它自己的形式。如果您然后在对话框中提交的表单,整个页面将重新呈现。

由于支持bean是视图范围的,以前的用户数据将被保留,只有用户名会,不管你在对话框中输入更新。

此外,还可以通过AJAX提交表单,给主要形式的ID和重新渲染。

例如

<rich:popupPanel id="popup" modal="true" resizeable="true" onmaskclick="#{rich:component('popup')}.hide()"> 
    <f:facet name="header">Simple popup panel</f:facet> 
    <h:form id="form2"> 
     <h:outputLabel for="username2" value="Username"/> 
     <h:inputText id="username2" value="#{register.user.username}" required="true"/> 
     <a4j:commandButton value="do" action="#{register.update}" render="form" oncomplete="#{rich:component('popup')}.hide()"/>     
    </h:form> 
</rich:popupPanel> 

我用它具有方便oncomplete属性A4J commandButton这里。您可以使用标准ajax标签上的onevent属性做类似的事情,但使用稍多的代码。

请注意,如果形式主要有验证错误,这是没有不可能从其他形式的更新任何反制措施(如果你进入的,你最好打开该特定情况新问题)。

+0

感谢回答,我明白你说的话(我认为:)),但未能得到正确的代码。我给了名为“form”的原始表单ID,并以其他形式将面板外部移动,但弹出窗口仍然不关闭,也没有更新调试点显示它正在触摸该方法,但屏幕流程不正确,请参阅我的更新问题结束。非常感谢您的回复。谢谢 –

+0

表格需要*在面板中,而不是围绕它。另外,按钮应该关闭对话框(例如通过脚本)。 –

+0

不能感谢你是伟大的。你和Blusc的工作原因:) –

0

它已经回答了,但我也有类似的问题。我的观点有<rich:dataTable>来显示数据,我想用<rich:popupPanel>中的选定项过滤结果。我有同样的问题:选择一个选项没有更新表(使用<a4j:ajax>事件)。

的弹出式窗口,默认情况下安装到<body>标签。所以,我加domElementAttachment="form"<rich:popupPanel>,它工作。

相关问题