0

我的表格有问题。该表单允许用户在一些文本框中输入信息,然后有 2 个按钮 - 搜索和重置。

表格代码:

<h:outputText value="Search by Author Name" styleClass="p" />

<div class="investigatorTab">

    <h:outputLabel for="firstName" rendered="true" value="First Name" />
    <h:inputText value="#{pubBacker.firstName}"></h:inputText>

    <h:outputLabel for="lastName" rendered="true" value="Last Name" />
    <h:inputText value="#{pubBacker.lastName}"></h:inputText>

</div>

<div class="buttons">
    <p:commandButton value="Submit" styleClass="submitButton" action="#{pubBacker.submit}" update="tabs" />
    <p:commandButton value="Reset" type="button" actionListener="#{pubBacker.clearEntries}" onclick="clearForm(this.form);" />
</div>

我遇到的问题是,如果我单击搜索按钮,它会将我带到结果页面,如果我返回搜索页面,它仍然具有我在文本字段中搜索的数据,因为 bean 是会话范围。我希望能够单击重置按钮并清除文本字段中的数据。

目前使用下面的代码,如果我点击重置按钮,搜索按钮不再起作用,并且在萤火虫控制台中它给了我这个错误

<?xml version='1.0' encoding='UTF-8'?>
<partial-response><error><error-name>class javax.faces.application.ViewExpiredException</error-name><error-message><![CDATA[viewId:/ccadmin/pubManagement.xhtml - View /ccadmin/pubManagement.xhtml could not be restored.]]></error-message></error></partial-response>

如果我onclick="clearForm(this.form);"从第二个中删除p:commandButton,表格可以正常工作。

任何想法为什么调用 JS 函数会使视图过期?

发布 Bean:

@ManagedBean(name="pubBacker")
@SessionScoped
public class Publication {

    public Publication() {}

    public void clearEntries() {
        new Publication();
    }
}
4

1 回答 1

1

您显然是通过盲目循环来清除表单的所有form.elements元素。这样,javax.faces.ViewState由 JSF 自动包含的隐藏输入字段也将被清除,因此 JSF 将检索一个空值,并且它将无法在服务器端找到关联的视图状态。这最终会以ViewExpiredException.

我建议不要使用JS来清除表单,而只使用JSF。如果您有一个代表表单的模型,那就更容易了。

<div class="investigatorTab">
    <h:outputLabel for="firstName" rendered="true" value="First Name" />
    <h:inputText value="#{pubBacker.pub.firstName}"></h:inputText>

    <h:outputLabel for="lastName" rendered="true" value="Last Name" />
    <h:inputText value="#{pubBacker.pub.lastName}"></h:inputText>
</div>

<div class="buttons">
    <p:commandButton value="Submit" styleClass="submitButton" action="#{pubBacker.submit}" update="tabs" />
    <p:commandButton value="Reset" action="#{pubBacker.clear}" process="@this" update="@form" />
</div>

(注意process="@this",这不会处理表单的输入值,因此不会验证它们)

public void clear() {
    pub = new Pub();
}
于 2012-02-03T18:28:44.973 回答