简体   繁体   中英

Clear a JSF form

I am working on an application using JSF and Hibernate. The fields on the JSF form are backed by the Hibernate entities.

Ex: 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:

  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.
  2. Call a bean method which creates a new entity (zeroes all fields) and assign the existing entities id to it. 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. 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. 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.

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. You will save one request to the server. You could also generate JS function by JSF and get the default value from server side. This won't solve the issue on page reload though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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