如何在不验证所有必须输入的情况下调用命令按钮?

9

我是一个有用的助手,可以为您翻译文本。

我试图从index.xhtml重定向到registerFirstTime.xhtml。

页面index.xhtml中的代码如下:

<h:form id="content" style="margin-top: 1em;">
    <p:panel id="panel" header="LOGIN">
        <p:messages id="msgs" globalOnly="true"/>
        <h:panelGrid columns="3">
            <h:outputLabel value="e-mail" />
            <p:inputText id="name" required="true" value="#{UserBean.email}"
                requiredMessage="Required: e-mail" display="icon">
            </p:inputText>
            <p:message id="msgName" for="name"/>
            <h:outputLabel value="Password" />
            <p:password id="password" required="true" value="#{UserBean.password}"
                requiredMessage="Required: Password" display="icon" feedback="false">
            </p:password>
            <p:message id="msgPassword" for="password"/>
        </h:panelGrid>
        <h:panelGrid columns="2">
            <p:commandButton value="Login" actionListener="#{UserBean.validate}" update="msgNombre, msgPassword, msgs"/>
            <h:commandButton value="Register for the first time" action="register"/>
        </h:panelGrid>
    </p:panel>
</h:form>

然而,faces-config.xml 中重定向的内容是:

<navigation-rule>
    <from-view-id>index.xhtml</from-view-id>
    <navigation-case>
        <from-outcome>register</from-outcome>
        <to-view-id>registerFirstTime.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

在输入名称和密码之前,无法进行重定向操作。 请问如何在不考虑必填字段的情况下进行记录重定向?谢谢!=)


你的意思是在提交时重定向而不填充值吗? - Nidheesh
是的,我需要创建一个重定向填充值(登录),但我还需要另一个重定向(注册)而不需要填充值。 - Víctor Pariente
我证明将h:commandButton放在另一个h:form中是可行的,但是否有可能将h:commandButton放在id为content的h:form中呢? - Víctor Pariente
2个回答

30

在命令按钮上添加 immediate="true",以跳过所有没有 immediate="true" 属性的输入字段的处理。

<h:commandButton value="Register for the first time" action="register" immediate="true" />

与具体问题无关的是,请注意,你在这里技术上并没有发送重定向。这基本上是发送了一个转发,即浏览器地址栏中的URL仍然是登录页面的URL。你需要将<redirect/>添加到<navigation-case>中。另外请注意,导航案例是JSF 1.x的遗留物。自从JSF 2.x以来,您可以执行“隐式导航”,只需指定视图ID作为结果即可。

<h:commandButton value="Register for the first time" action="registerFirstTime?faces-redirect=true" immediate="true" />
这样你就可以完全摆脱导航案例。

7

@BalusC 的解决方案应该能解决问题,这是他一贯的风格。但是我想指出一些事情,因为你似乎在使用 Primefaces。这两个问题与问题无关,但可能有助于您在某些时候解决问题。

首先,您可以使用隐式导航(JSF2中引入)。这样,您就不需要在 faces-config.xml 文件中定义所有导航规则了(我正在处理一个旧的 JSF 1.2 项目,并且厌烦需要为每个东西定义导航角色)。以下是如何实现:

<h:commandButton value="首次注册" action="registerFirstTime.xhtml?faces-redirect=true"/>

faces-redirect 参数强制重定向而不是转发,如果需要的话。

另外,假设您想正确处理某些值,但不是所有值。在这种情况下,您可以使用 p:commandButton 或 p:ajax 的 process 属性。例如

<h:commandButton value="首次注册" action="registerFirstTime.xhtml?faces-redirect=true" process="@this, name"/>

这将使 JSF 仅处理按钮(@this)和 id="name" 组件(您的电子邮件字段)。同样,它可能不适用于问题,但这是我经常使用的东西。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接