简体   繁体   English

清除JSF表单

[英]Clear a JSF form

I am working on an application using JSF and Hibernate. 我正在使用JSF和Hibernate开发应用程序。 The fields on the JSF form are backed by the Hibernate entities. JSF表单上的字段由Hibernate实体支持。

Ex: value=#{bean.entity.value} 例如:值=#{bean.entity.value}

There is a Clear button which should empty all the fields on the form. 有一个清除按钮,它应该清空表单上的所有字段。 There are 3 ways to do this: 有3种方法可以做到这一点:

  1. Call a javascript function which loops through all the fields on the form and sets default value based on the type of field - text/checkbox/dropdown. 调用一个javascript函数,该函数遍历表单上的所有字段并根据字段类型-文本/复选框/下拉列表设置默认值。
  2. Call a bean method which creates a new entity (zeroes all fields) and assign the existing entities id to it. 调用一个bean方法,该方法创建一个新实体(将所有字段清零)并为其分配现有的实体ID。 I hoped Hibernate would then update the row with that id, but instead it is creating a new row since it is a detached entity. 我希望Hibernate然后使用该ID更新该行,但由于它是一个分离的实体,因此它正在创建一个新行。 Is there a way to fix this? 有没有办法解决这个问题?
  3. The most straight forward way is to call a bean method and manually set default values for all the fields in the entity. 最直接的方法是调用bean方法并为实体中的所有字段手动设置默认值。 The problem with this is that there are too many fields and each time any change is made on the front end, I need to update the bean method accordingly. 这样做的问题是字段太多,并且每次在前端进行任何更改时,我都需要相应地更新bean方法。

Which is the most appropriate way to do this? 哪种方法最合适?

If no business is required to calculate the default value I would use JS function to reset fields. 如果不需要任何业务来计算默认值,我将使用JS函数重置字段。 You will save one request to the server. 您将一个请求保存到服务器。 You could also generate JS function by JSF and get the default value from server side. 您还可以通过JSF生成JS函数,并从服务器端获取默认值。 This won't solve the issue on page reload though. 但是,这不能解决页面重新加载的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM