jsf-2 - 如何从richfaces弹出窗口刷新父jsf页面

标签 jsf-2 richfaces java-ee-6

我有一个包含几个字段的 JSF 页面。我关注了这个 tutorial来自 BalusC,一切都很好。然后我为它添加了 RichFaces 支持,它工作正常。我可以看到弹出窗口等工作。

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

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 是 View 范围的,因此将保留以前的用户数据,并且只会使用您在对话框中输入的任何内容更新用户名。

此外,您可以通过 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属性。你可以用 onevent 做类似的事情。属性在标准的 ajax 标签上,但使用的代码稍微多一些。

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

关于jsf-2 - 如何从richfaces弹出窗口刷新父jsf页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7133393/

相关文章:

java - Java编译器如何允许这样的类定义

jsf - @ManagedProperty - 将一个请求范围的 bean 注入(inject)另一个请求范围的 bean

java - 以编程方式添加 p :resetInput (tag handler) onto a p:commandButton

java - 无需 session bean 即可优雅地处理属性

java - CDI 是否为默认生产者提供 API?

jboss - 将注释从 JBoss Seam 更改为 CDI (JEE6)

java - JSF 2.0 注释不起作用

spring - p :remoteCommand only update ="@all" PF5

java - 如何在加载时隐藏 RichFaces 组件?

internet-explorer - 隐藏的 popupPanel 内的 Richfaces dataTable 渲染不良