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

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

在文本框中输入值并提交后,该值仍会出现在文本框中。提交文本框后,我需要清除其内容。我怎样才能做到这一点?

最佳答案

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

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类中...
    public String saveProduct(){
    try {
        product.save();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

从view/jsp进行的方法调用如下所示:
<h:commandButton value="Save" action="#{productController.saveProduct}"/>

09-30 22:38
查看更多