Clear JSF form input values after submitting

前端 未结 5 1979
情歌与酒
情歌与酒 2020-11-27 07:03

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?



        
5条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 07:33

    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`
    

    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;
    }
    

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

    
    

提交回复
热议问题