表单 – 提交后清除JSF表单输入值
发布时间:2020-12-14 18:52:26 所属栏目:资源 来源:网络整理
导读:如果有一个表单,并且有一个文本框和一个按钮,您在提交表单后如何清除文本框的内容? h:inputText id="name" value="#{bean.name}" / h:commandButton id="submit" value="Add Name" action="#{bean.submit}" / 在文本框中输入值并提交后,该值仍会显示在文本
如果有一个表单,并且有一个文本框和一个按钮,您在提交表单后如何清除文本框的内容?
<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}"/> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |