本文主要解释如何 使用Bean Shell 表单绑定器 从表单表格中 加载/读取 数据 . 如果您需要从多个表中加载/存储表单数据,这可能非常方便。

加载绑定器 -> Bean Shell表单绑定器

import org.joget.apps.form.model.*;
import org.joget.apps.form.service.*;
import java.sql.*;
import java.util.*;
 
FormRowSet f = new FormRowSet();
f.setMultiRow(true);
 
//Get Joget's current datasource configs
DataSource ds = (DataSource) AppUtil.getApplicationContext().getBean("setupDataSource");
 
con = ds.getConnection();
 
if (!con.isClosed()) {
    //Get the URL parameter
    String recordId = "#requestParam.id#";
 
    //Here you can query from one or multiple tables using JOIN etc
    String sql = "SELECT * FROM your_table_name WHERE id=?";
    PreparedStatement stmt = con.prepareStatement(sql);
    stmt.setString(1, recordId);
 
    //Execute the SELECT SQL statement
    ResultSet rs = stmt.executeQuery();
         
    //Get value from columns of record(s)
    while (rs.next()) {
        FormRow r1 = new FormRow();
        r1.put("gridColumn1", rs.getString(1));
        r1.put("gridColumn2", rs.getString(2));
        r1.put("gridColumn3", rs.getString(3));
        f.add(r1);
    }
}
 
return f;

存储绑定器 -> Bean Shell 表单绑定器

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.form.model.Form;
import org.joget.apps.form.model.Element;
import org.joget.apps.form.model.FormData;
import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
import org.joget.apps.form.service.FormUtil;
import org.joget.commons.util.UuidGenerator;
 
public saveGridRows(Element element, FormRowSet rows, FormData formData) {
     
    String recordId = null;
    Connection con = null;
     
    try {
        //Get Joget's current datasource configs
        DataSource ds = (DataSource) AppUtil.getApplicationContext().getBean("setupDataSource");
 
        con = ds.getConnection();
         
        if(!con.isClosed()) {
            //To generate new record IDs for storing into child table
            UuidGenerator uuid = UuidGenerator.getInstance();
             
            //Iterate to add new records
            Iterator i= rows.iterator();
            while (i.hasNext()) {
                FormRow row = (FormRow) i.next();
                 
                String pId = uuid.getUuid();
                String gridColumn1 = row.get("gridColumn1");
                String gridColumn2 = row.get("gridColumn2");
                String gridColumn3 = row.get("gridColumn3");
                 
                String insertSql = "INSERT INTO your_table_name (id,gridColumn1,gridColumn2,gridColumn3) VALUES (?,?,?,?);";
                 
                PreparedStatement stmtInsert = con.prepareStatement(insertSql);
                 
                stmtInsert.setString(1, pId);
                stmtInsert.setString(2, gridColumn1);
                stmtInsert.setString(3, gridColumn2);
                stmtInsert.setString(4, gridColumn3);
                 
                //Execute SQL statement
                stmtInsert.executeUpdate();
            }
        }
    } catch (Exception ex) {
        LogUtil.error("Your App/Plugin Name", ex, "Error storing using jdbc");
    } finally {
        try {
            if (con != null) {
                con.close();
            }
        } catch (Exception ex) {
            LogUtil.error("Your App/Plugin Name", ex, "Error closing the jdbc connection");
        }
    }
}
 
//Process and store grid rows
saveGridRows(element, rows, formData);


表格设计

正确的表格设计应如下所示:

 

部分load binder beanshell脚本:

import org.joget.apps.form.model.FormRow;
import org.joget.apps.form.model.FormRowSet;
 
FormRowSet f = new FormRowSet();
f.setMultiRow(true);
 
FormRow r1 = new FormRow();
r1.put("gridColumn1", your_value);
r1.put("gridColumn2", your_value);
r1.put("gridColumn3", your_value);
f.add(r1);
 
FormRow r2 = new FormRow();
r2.put("gridColumn1", your_value);
r2.put("gridColumn2", your_value);
r2.put("gridColumn3", your_value);
f.add(r2);
  
return f;
  • No labels