如何从richfaces弹出刷新父jsf页面



我有一个包含几个字段的JSF页面。我遵循了BalusC的这个教程,一切都很好。然后我添加了RichFaces支持,这是工作良好。我可以看到弹出窗口等工作

现在我要做的是,一旦一个人已经注册,我想在弹出框中显示姓名(这也很好,我可以在弹出框中看到)。然后我希望能够改变弹出框内的名称,并有反映在父,但我不知道如何做到这一点。请看一看。

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

根据Arjan 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>

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>

我在这里使用了A4J commandButton,它有一个方便的oncomplete属性。您可以对标准ajax标记上的onevent属性执行类似的操作,但是要使用更多的代码。

请注意,如果主表单有验证错误,则没有任何应对措施无法从其他表单更新(如果遇到这种情况,最好针对该特定情况打开一个新问题)。

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

默认情况下,弹出框是附在<body>标签上的。所以,我把domElementAttachment="form"添加到<rich:popupPanel>,它工作了。

最新更新