【问题标题】:Hide h:outputText element if there was no user input如果没有用户输入,则隐藏 h:outputText 元素
【发布时间】:2023-03-29 14:55:02
【问题描述】:
<h:form id="aform">

    <p:growl id="debug-growl" showDetail="true" sticky="false" />

    <p:inputText id="expression" value="#{debug.expression}" required ="true" />

    <p:commandButton update="debug-growl" value="Process" action="#{debug.myaction}" />

    <h:outputText value="Source regular expression: #{debug.expression}" rendered="#{not empty debug.expression}" />

</h:form>

豆子

@ManagedBean
@ViewScoped
public class Debug implements Serializable {

    private String expression; //getter & setter is present

当我输入值时,它会在h:outputText 元素中提交后显示。 但是,如果我输入空值(这是错误的),那么在 h:outputText 元素中仍会显示先前的值。

没有提交值时如何隐藏“h:outputText”?

【问题讨论】:

  • 尝试更改您的@ViewScoped 注释
  • @JokerTheFourth:为什么?
  • 要隐藏某些内容,您可以使用 DisabledRendred 在这种情况下(渲染)它将是不可见的。

标签: jsf jsf-2 primefaces jsf-2.2


【解决方案1】:

所以我发现上面的代码有 2 个问题。

  1. 您没有更新命令按钮单击时的 h:outputText。您需要向 h:outputText 添加一个 ID,并将其作为命令按钮的更新添加

    <p:commandButton update="debug-growl someText" value="Process" action="#{debug.myaction}" />
    
    <h:outputText id = "someText" value="Source regular expression: #{debug.expression}" rendered="#{not empty debug.expression}" />
    
  2. inputText 上的必需 ="true" 不允许将空值提交给服务器。因此 h:outputText 永远不会为空,因此该值将始终被呈现。为了解决这个问题,我会在服务器上进行验证。

    JSF

    删除 required="true" 标签

    <p:inputText id="expression" value="#{debug.expression}"/>
    

    Java

    public void myAction(){
    
        //check to make sure inputText isnt null/blank first
        if(StringUtils.isBlank(expression))
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Error", "Must provide value"));
        else{
             //business logic
        }
    }
    

【讨论】:

    猜你喜欢
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 2021-10-07
    • 2014-03-08
    • 1970-01-01
    相关资源
    最近更新 更多