简体   繁体   English

如何刷新已在jsf中预先插入的表单值?

[英]How to refresh form values which have been prevoiusly inserted in jsf?

In my application, I am adding form data in database on click of submit button.But the problem is, when user refreshes the page, the data added one more time into the database and so on... 在我的应用程序中,单击提交按钮后,我正在数据库中添加表单数据,但是问题是,当用户刷新页面时,数据又向数据库中添加了一次时间,依此类推...

What can be the solution for this, please some body give me good link/solution for this problem... 有什么解决方案,请有人给我很好的链接/解决方案...

Thanks a lot. 非常感谢。

Code : 代码:

<h:commandButton value="Submit" action="#{co.createAction()}" />

public String createAction() {
  if (login.registerUser().equals("success")) {
    return "index?faces-redirect=true";
  } else {
    return "failure";
  }
}

public String registerUser() {
  if (serv.createRegUser(reg)) {
    return "success";
  } else {
    return "failed";
  }
}

public boolean createRegUser(Registration reg) { 
  try { 
    initJpa(); 
    regJpa.create(reg); 
    return true; 
  } catch(Exception e) { 
    e.printStackTrace(); 
    return false; 
  } 
}

Also avoid using void methods in JSF. 还要避免在JSF中使用void方法。 If you want to return back to same page use the same page viewid. 如果要返回同一页面,请使用相同的页面viewid。 Try to put the following code: 尝试输入以下代码:

public String submit() {
    // ...

    return "viewid?faces-redirect=true";
}

The reference is taken from this link . 该引用来自此链接

This could be caused by several factors : 这可能是由以下几个因素引起的:

1 - Your function adding the item in the database is called each time the view is displayed. 1-每次显示视图时,都会调用将项目添加到数据库中的函数。

This could be caused by a @PostConstruct annotation on the aforesaid method. 这可能是由上述方法上的@PostConstruct注释引起的。 This could also be caused by an element calling this method from the view. 这也可能是由于从视图中调用此方法的元素引起的。

2 - Your method for inserting object in database is persisting 2 objects 2-您在数据库中插入对象的方法将保留2个对象

3 - Your form is submitted more than once 3-您的表格已提交多次

4 - You inserted a second object if the DB when you observed 4-如果观察到DB,则插入了第二个对象

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

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