JSF中的验证错误后,值丢失 [英] values lost after validation error in JSF

查看:84
本文介绍了JSF中的验证错误后,值丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两页.搜索页面是接受用户输入的第一页.第二页显示数据表中的结果集.第二页有3个结果集面板,可在单个页面中更新和创建所有面板.根据要单击的按钮,我将面板显示为真.

I have two pages. Search page is the first page that takes user inputs. Second page shows the result set in datatable. Second page has 3 panel for resultset, update and create all in single page. Depending upon the which button being clicked, I am rendering panels true and false.

<h:panelGroup styleClass="panelGroup"
                id="resultSet" rendered="#{bean.truefalse1}">
.
.
</h:panelGroup

<h:panelGroup styleClass="panelGroup"
                id="updateForm" rendered="#{bean.truefalse2}">
.
.
</h:panelGroup


<h:panelGroup styleClass="panelGroup"
                id="createForm" rendered="#{bean.truefalse3}">
.
.
</h:panelGroup>

在搜索页面中,我将这些创建和更新面板设置为false并仅显示结果集.单击结果集中的行后,我将显示 updateForm面板,但将create panel设置为false.

From the search page I am setting these create and update panels to false and displaying only resultset.After the row from the result set is clicked I am showing updateForm panel but keeping create panel to false.

但是这里的问题是,如果存在验证错误,则从搜索页面设置的属性将丢失,并且将显示所有面板.

But here the problem is, If there is validation error, then the property that was set from search page is being lost and all the panels are shown.

由于我没有导航到其他页面,如何获取先前在搜索页面上设置的值(布尔值true或false).

How do I get the value(boolean true or false) that was set from search page previously, since I am not navigating to different page.

我在第二堂课中有布尔型属性的getter和setter方法.我什至尝试保留隐藏字段(即从搜索页面设置的布尔属性). 验证错误后,不应恢复所有提交的值.或者只是我们在表格中键入的内容.

I have getters and setters for boolean property in second class. I even tried keeping hidden fields(i.e the boolean property that was set from search page). Shouldn't all the submitted values be recovered after validation error. Or just the ones we type in the form.

什么是最佳解决方案?

任何帮助都将受到高度赞赏!!!

Any help is highly appreciated!!!

推荐答案

您确实需要将完全相同的布尔属性传输到下一个请求.从理论上讲,您可以使用<h:inputHidden value="#{bean.boolean1}" />,但是不幸的是,这些值只能在更新模型值阶段进行设置,而实际上您需要在应用请求值阶段进行设置.此外,当发生验证错误时,它们也会丢失.

You indeed need to transfer the very same boolean properties to the next request. You can in theory use <h:inputHidden value="#{bean.boolean1}" /> for this, but unfortunately those will only be set during update model values phase, while you actually need it to be available during apply request values phase. Besides, they will also go lost when a validation error occurs.

有三种方法可以解决h:inputHidden的这种非直觉的行为(我曾经在Mojarra问题列表中针对它提出过一个错误,但似乎并未对此做任何事情).

There are three ways to fix this non-intuitive behaviour of h:inputHidden (I've ever filed a bug against it at the Mojarra issue list, but they didn't seem to do anything with it).

首先是在h:inputHidden上使用binding:

<h:inputHidden binding="#{bean.hidden1}" />

但是,这需要更改在后备bean代码中获取/设置布尔值的方式.例如:

This however requires changes in the way you get/set the boolean values in the backing bean code. For example:

private HtmlInputHidden hidden1 = new HtmlInputHidden(); // +getter +setter.

public void setBoolean1(boolean boolean1) {
    hidden1.setValue(boolean1);
}

public boolean getBoolean1() {
    return (Boolean) hidden1.getValue();
}

第二个方法是使用战斧t:saveState.

<t:saveState value="#{bean.boolean1}" />

主要优点是您不需要更改后备bean代码中的任何内容.它将在应用请求值阶段之前尽早恢复该值.您只需要添加额外的库(如果尚未完成的话),但是Tomahawk所提供的优势远远超过t:saveState,例如在基本JSF实现中缺少组件/功能t:inputFileUploadt:dataListt:dataTable preserveDataModel="true"t:selectOneRadio layout="spread"等,值得付出努力.

The major advantage is that you don't need to change anything in the backing bean code. It will restore the value early before the apply request values phase. You only need to add extra libraries if not done yet, but as Tomahawk provides much more advantages than only the t:saveState, such as the in basic JSF implementation missing components/features t:inputFileUpload, t:dataList, t:dataTable preserveDataModel="true", t:selectOneRadio layout="spread" and so on, it is worth the effort.

第三种方法是将它们存储在会话范围的Bean中,但实际上您不想对请求范围的变量执行此操作.它只会给"wtf?"最终用户在同一会话中打开多个选项卡/窗口时会遇到这种情况.

The third way is to store them in a session scoped bean, but you actually don't want to do that for request scoped variables. It would only give "wtf?" experiences when the enduser has multiple tabs/windows open in the same session.

根据评论,这是第二种方法的 SSCCE :

as per the comments, here's an SSCCE of the second way:

JSF页面:

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<f:view>
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        </head>
        <body>
            <h:form id="form">
                <h:inputHidden binding="#{myBean.hidden}" converter="javax.faces.Boolean" />
                <h:commandButton value="submit" action="#{myBean.submit}"/>
                <h:outputText value="Current boolean value: #{myBean.hidden.value}" />
            </h:form>
        </body>
    </html>
</f:view>

MyBean类:

package mypackage;

import javax.faces.component.html.HtmlInputHidden;

public class MyBean {

    private HtmlInputHidden hidden = new HtmlInputHidden();

    public void submit() {
        if (hidden.getValue() == null) {
            hidden.setValue(true); // Set to true on 1st submit.
        } else {
            hidden.setValue(!((Boolean) hidden.getValue())); // Toggle true/false.
        }
    }

    public HtmlInputHidden getHidden() {
        return hidden;
    }

    public void setHidden(HtmlInputHidden hidden) {
        this.hidden = hidden;
    }

}

faces-config.xml的相关部分:

<managed-bean>
    <managed-bean-name>myBean</managed-bean-name>
    <managed-bean-class>mypackage.MyBean</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

操场上的环境是Tomcat 6.0.20上的JSF 1.2_13.

Playground environment is JSF 1.2_13 on Tomcat 6.0.20.

这篇关于JSF中的验证错误后,值丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆