当前位置: 首页 > 知识库问答 >
问题:

PrimeFaces:不提交条件呈现的组件

梁烨烨
2023-03-14
<h:form id="form">
    <p:layout id="layout">
        ...
        <p:layoutUnit id="layoutUnit">
            ...
            <p:panel id="panel">
                <p:outputPanel id="container1">
                    <p:selectOneMenu id="value1" value="#{bean.value1}">
                        <f:selectItem itemValue="1"/>
                        <f:selectItem itemValue="2"/>
                        <f:selectItem itemValue="3"/>
                        <f:selectItem itemValue="18"/>
                        <p:ajax event="change" update="container2"/>
                    </p:selectOneMenu> 
                </p:outputPanel>

                <p:outputPanel id="container2">
                    <p:inputText id="value2" 
                                 value="#{bean.value2}" 
                                 rendered="#{bean.value1 eq 18}"
                                 >
                    </p:inputText>
                </p:outputPanel>
            </panel>

            <div id="buttons">
                <p:commandButton id="commandButton" action="#{bean.save}" value="save" />
            </div>
        </layoutUnit>
    </layout>
</form>
    JSF的
  • @ViewScoped不可用,因为它与CDI冲突
    • 提交表单时,JSF动态呈现组件的值变为空
    • 有条件呈现的输入组件不更新值
    • 未调用CommandButton/CommandLink/Ajax操作/Listener方法或未更新输入值

    我能想到导致这种行为的几种情况:

    1. “rendered”似乎是基于backing bean中的值重新计算的,而不是UI中给出的新值(如果默认为1,则提交时再次为1,而不是18)。因此,组件不会提交。
    2. 添加的组件未正确添加到表单中,因此未提交。

共有1个答案

梁丘扬
2023-03-14

除了使用@Named之外,还使用CDI的@ConversationScoped是一种解决方案。该作用域的工作方式与JSF中的@ViewScoped相当。

为了使其工作,我只添加了这里示例中的代码。所以简而言之:

Bean.java

@Named
@ConversationScoped
public class Bean implements Serializable{

    @Inject
    private Conversation conversation;

    Bean values, getters & setters

    public void initConversation(){
        if (!FacesContext.getCurrentInstance().isPostback() && conversation.isTransient()) {         
            conversation.begin();
        }
    }

    public String endConversation(){
        if(!conversation.isTransient()){
            conversation.end();
        }
    }

    public String navigateAwayFromScreen(){
        endConversation();
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<!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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:ui="http://java.sun.com/jsf/facelets">

    <f:event listener="#{bean.initConversation}" type="preRenderView"/>

    <h:form id="form">
        <p:layout id="layout">
            ...
            <p:layoutUnit id="layoutUnit">
                ...
                <p:panel id="panel">
                    <p:outputPanel id="container1">
                        <p:selectOneMenu id="value1" value="#{bean.value1}">
                            <f:selectItem itemValue="1"/>
                            <f:selectItem itemValue="2"/>
                            <f:selectItem itemValue="3"/>
                            <f:selectItem itemValue="18"/>
                            <p:ajax event="change" update="container2"/>
                        </p:selectOneMenu> 
                    </p:outputPanel>

                    <p:outputPanel id="container2">
                        <p:inputText id="value2" 
                                     value="#{bean.value2}" 
                                     rendered="#{bean.value1 eq 18}"
                                     >
                        </p:inputText>
                    </p:outputPanel>
                </panel>

                <div id="buttons">
                    <p:commandButton id="commandButton" action="#{bean.navigateAwayFromScreen}" value="Go away!" />
                </div>
            </layoutUnit>
        </layout>
    </form>
</html>
 类似资料:
  • 我正在编写一组定制的PrimeFaces组件,使用PrimeFaces5.0,并在JBoss EAP6.2中运行。 null 2.2在myFaceStest.taglib.xml中,我定义了输入标记: 2.3在input.java(我的自定义组件)中,我执行以下操作: 2.4此组件的呈现器包含以下内容: 给定这种设置,为什么自定义组件不会呈现?我的jboss日志中没有任何内容,即使日志级别设置为d

  • 对于在其生命周期的某个点隐藏的组件,呈现它的最佳方式是什么?1)渲染组件,但不显示它(显示:无)。2)只在需要时渲染组件。什么对性能更好?如果组件的道具和状态稍后更新,是否最好让组件存在,但隐藏在虚拟DOM中? 或者这个:

  • 如何在样式组件中使用条件渲染来设置我的按钮类在React中使用样式组件? 在css中,我会这样做: 如果我尝试使用'

  • 如何使用JSX进行条件渲染? 例如,我这里有div,如果props的值为null,我想呈现文本“不知道”,否则如果props不等于null,则呈现props。 例如: 我试过了,但没有成功。知道我做错了什么吗? 提前感谢!

  • 除了项目的配置之外,我所做的没有比展示柜上说的更多。以下是我的设置摘要: > 我在Windows x64上使用Apache Tomcat 8.0.39 Firefox 50.0.2和Chrome版本55.0.2883.75m(64位)显示相同的结果(只有当前选定的图像可见) 我使用的是Java8 ,这些是项目的依赖项 这是我的backing bean:

  • 问题内容: 我是新来的世界,我有这样的话: 然后单击,您将被打印在控制台上。现在将行更改为: 现在点击该按钮,我希望它会被渲染。但事实并非如此。 我不确定为什么会这样。请注意,我在方法中有上面的代码。 问题答案: 您可能希望有一个状态组件,该状态组件在单击按钮之后在按钮旁边显示另一个组件。您需要做的就是跟踪按钮是否被单击: