提交后清除JSF表单输入值 [英] Clear JSF form input values after submitting

查看:146
本文介绍了提交后清除JSF表单输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有表单,并且有一个文本框和一个按钮,您在提交表单后如何清除文本框的内容?

If there's a form, and has a textbox and a button, how do you erase the content of the textbox after you submit the form?

<h:inputText id="name" value="#{bean.name}" />          
<h:commandButton id="submit" value="Add Name" action="#{bean.submit}" />

在文本框中输入值并提交后,该值仍显示在文本框中。一旦提交,我需要清除文本框的内容。我可以如何实现这一点?

After I enter a value in the textbox and submit, the value still appears in the textbox. I need to clear the content of the textbox once its been submitted. How can I achieve this?

推荐答案

您可以从提交表单时调用的Bean方法中清除表单;

You can clear the form from the Bean method that gets called when the form is submitted;`

private String name;
    private String description;
    private BigDecimal price;

/*----------Properties ------------*/
/*-----Getter and Setter Methods---*/

public void save()throws SQLException{
String sql = "INSERT INTO tableName(name,description,price) VALUES (?,?,?)";
    Connection conn = ds.getConnection();
    try {

        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, getName());
        pstmt.setString(2, getDescription());
        pstmt.setBigDecimal(3, getPrice());

        pstmt.executeUpdate();
    } catch (SQLException e) {
        e.getMessage();
        e.toString();
    }finally{
        conn.close();
        clear();
    }

}//End Save Method

public void clear(){
    setName(null);
    setDescription(null);
    setPrice(null);
}//end clear`

注意clear()方法是从保存方法的所有操作完成后的保存方法。作为一个选项,只有当方法操作成功时才​​可以执行清除操作...下面的方法放置在ProductController类中...

Notice that the clear() method is called from the save method after all the operations of the save method is complete. As an option you could perform the clearing only if the methods operation was successful...The method below is placed in the ProductController Class...

    public String saveProduct(){
    try {
        product.save(); 
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

来自view / jsp的方法调用如下所示: p>

The method call from the view/jsp would look like the Following:

<h:commandButton value="Save" action="#{productController.saveProduct}"/>

这篇关于提交后清除JSF表单输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆