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

查看:27
本文介绍了提交后清除 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`

注意,在save方法的所有操作完成后,会从save方法中调用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;
}

来自视图/jsp 的方法调用如下所示:

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

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

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

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