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

Víc*_*nte 9 jsf redirect required-field primefaces

我正在尝试从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>
Run Code Online (Sandbox Code Playgroud)

而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>
Run Code Online (Sandbox Code Playgroud)

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

Bal*_*usC 28

添加immediate="true"到命令按钮,该按钮应跳过没有该immediate="true"属性的所有输入字段的处理.

<h:commandButton value="Register for the first time" action="register" immediate="true" />
Run Code Online (Sandbox Code Playgroud)

具体问题无关,请注意您在技术上不在此处发送重定向.这基本上发送了一个转发,即浏览器地址栏中的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" />
Run Code Online (Sandbox Code Playgroud)

这样你就可以完全摆脱导航案例.


And*_*dre 6

@BalusC解决方案应该像往常一样.但是我想指出一些事情,因为你似乎在使用Primefaces.两者都与问题无关,但可能在某些方面帮助你.

首先,您可以使用隐式导航(在JSF2中引入).这样您就不需要在faces-config.xml文件中定义所有导航规则(我在旧的JSF 1.2项目上工作,并且讨厌为所有内容定义导航角色的需要).这是你如何做到的:

<h:commandButton value="Register for the first time" action="registerFirstTime.xhtml?faces-redirect=true"/>

faces-redirect参数强制重定向而不是转发,以防您需要.

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

<h:commandButton value="Register for the first time" action="registerFirstTime.xhtml?faces-redirect=true" process="@this, name"/>

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