成功提交后重置表单的输入字段(Reset input fields of a form after successful submit)

编程入门 行业动态 更新时间:2024-10-27 08:39:25
成功提交后重置表单的输入字段(Reset input fields of a form after successful submit)

我有一个创建新Employee的表单。 我的支持bean是@SessionScoped 。 当我创建第一个员工时,一切都进展顺利。 但是,当我即将创建第二个员工时,表单仍会在输入字段中显示第一个员工的属性。

如何在不改变bean范围的情况下重置它们? 范围对于其他目的是强制性的。 我使用托管Bean(控制器),我有“创建员工”

public String createEmploye() { employe = new Employe(); employe.setId(this.id); employe.setNom(this.nom); employe.setPrenom(this.prenom); employe.setNum_telephone(this.num_telephone); employe.setAdresse(this.adresse); employe.setNum_poste(this.num_poste); employeBean.addEmploye(employe); employe.setNom(""); return "ListEmployes.xhtml?faces-redirect=true"; // return ("ListEmployes.xhtml"); }

I have a form which creates a new Employee. My backing bean is @SessionScoped. When I create the first employee, everything went well. However, when I'm about to create the second employee, the form still displays the properties of the first employee in the input fields.

How do I reset them without changing the scope of the bean? The scope is mandatory for other purposes. i use a managed Bean ( controller) where i have "Create employe"

public String createEmploye() { employe = new Employe(); employe.setId(this.id); employe.setNom(this.nom); employe.setPrenom(this.prenom); employe.setNum_telephone(this.num_telephone); employe.setAdresse(this.adresse); employe.setNum_poste(this.num_poste); employeBean.addEmploye(employe); employe.setNom(""); return "ListEmployes.xhtml?faces-redirect=true"; // return ("ListEmployes.xhtml"); }

最满意答案

将Employee实例保存在DB中后重新创建它。

public void save() { service.save(employee); employee = new Employee(); // <--- Just add this line. }

具体问题无关 ,但我强烈建议重新考虑你的bean设计。 难道它不应该分成两个豆? 一个请求/视图作用于表单本身,另一个会话作用于实际会话作用域数据,这些数据将在请求/视图作用域中注入。 通过这种方式,您可以在保存之后只执行重定向到同一视图,以便从一个干净的表单开始(并且还有一个额外的好处,即当您在提交后刷新页面时,同一个员工不会在数据库中重复) 。

也可以看看:

如何选择合适的bean范围?

根据更新进行更新,似乎您正在复制/展平辅助bean中Employee所有属性,而不是让表单直接引用它们。 我强烈建议不要将模型属性复制/压平到控制器中。

@ManagedBean @SessionScoped public class Manager { private Employee employee = new Employee(); @EJB private EmployeeService service; public void createEmployee() { service.create(employee); employee = new Employee(); } public Employee getEmployee() { return employee; } }

<h:inputText value="#{manager.employee.firstname}" /> <h:inputText value="#{manager.employee.lastname}" /> <h:inputText value="#{manager.employee.telephone}" /> <h:inputText value="#{manager.employee.street}" /> ...

Recreate the Employee instance after saving it in the DB.

public void save() { service.save(employee); employee = new Employee(); // <--- Just add this line. }

Unrelated to the concrete problem, I however strongly recommend to reconsider your bean design. Shouldn't it rather be split into two beans? One request/view scoped for the form itself and another session scoped one for the real session scoped data which get injected in the request/view scoped one. This way you can after the save just perform a redirect to the same view in order to start with a clean form (and have the additional benefit that the very same employee doesn't get duplicated in the DB when you refresh the page after submit).

See also:

How to choose the right bean scope?

Update as per the update, it seems that you're duplicating/flattening all properties of Employee in the backing bean instead of letting the form refer them directly. I strongly recommend to not duplicate/flatten the model properties into the controller.

@ManagedBean @SessionScoped public class Manager { private Employee employee = new Employee(); @EJB private EmployeeService service; public void createEmployee() { service.create(employee); employee = new Employee(); } public Employee getEmployee() { return employee; } }

with

<h:inputText value="#{manager.employee.firstname}" /> <h:inputText value="#{manager.employee.lastname}" /> <h:inputText value="#{manager.employee.telephone}" /> <h:inputText value="#{manager.employee.street}" /> ...

更多推荐

employe,bean,员工,创建,电脑培训,计算机培训,IT培训"/> <meta name="descriptio

本文发布于:2023-08-07 20:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465667.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   表单   提交后   Reset   input

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!