form表单域的动态设计的代码实现-customized tab information 模块代码开发总结(2)

I discussed the dynamic form fields design in the fromer charter.
Now i will discuss the java code which related the dynamic form fileds 
form interface as follows:
form表单域的动态设计的代码实现-customized tab information 模块代码开发总结(2)_第1张图片
hris_dynamictable_HTC.java
get dynamiclist
dynamicList = dynamicDao.getDynamicTableListWithDate(comp_id, p_id,currentSEQ, currentEffDate);

dynamicDao.getDynamicTableListWithDate()

public List<DynamictableBean> getDynamicTableListWithDate(String comp_id, String p_id,Integer currentSEQ,String currentEFFDate) throws ParseException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
Statement st2 = null;
ResultSet rs2 = null;
FormatDate fd = new FormatDate();
String   sql =
" select "
+ " field_id,table_id,column_id,primary_key, field_label, label_row,label_column, label_column_span, "
+ " label_display_class, value_row, value_column, value_column_span,"
+ " value_display_class, field_type, component_length, textbox_max_input,"
+ " default_value,pay_code_type, data_type, decode(not_null,1,1,0) not_null, special_validation_logic_class,"
+ " alignment,bgcolor from hris_dynamictable where comp_id = '"+comp_id+"' order by label_row,label_column";
                 //key sql
                //the results of this query will be show in the end of these codes
                //------------------------end
List<DynamictableBean> tempList = new ArrayList<DynamictableBean>();
try {
conn = DB.getConnection();
st = conn.createStatement();
rs = st.executeQuery(sql);
//tempList = DB.selectToList(sql, new Object[] {}, DynamictableBean.class);
while (rs.next()) {
DynamictableBean  dBean = new DynamictableBean();
dBean.setField_id(rs.getString(1));
dBean.setTable_id(rs.getString(2));
dBean.setColumn_id(rs.getString(3));
dBean.setPrimary_key(rs.getBoolean(4));
dBean.setField_label(rs.getString(5));
dBean.setLabel_row(rs.getInt(6));
dBean.setLabel_column(rs.getInt(7));
dBean.setLabel_column_span(rs.getInt(8));
dBean.setLabel_display_class(rs.getString(9)==null?"":rs.getString(9));
dBean.setValue_row(rs.getInt(10));
dBean.setValue_column(rs.getInt(11));
dBean.setValue_column_span(rs.getInt(12));
dBean.setValue_display_class(rs.getString(13)==null?"":rs.getString(13));
dBean.setField_type(rs.getInt(14));
dBean.setComponent_length(rs.getInt(15));
dBean.setTextbox_max_input(rs.getInt(16));
dBean.setDefault_value(rs.getString(17));
dBean.setPay_code_type(rs.getString(18));
dBean.setData_type(rs.getInt(19));
dBean.setNot_null(rs.getBoolean(20));
dBean.setSpecial_validation_logic_class(rs.getString(21));
dBean.setAlignment(rs.getString(22)==null?"":rs.getString(22));
dBean.setBgcolor(rs.getString(23));
//** for value
                                //
String vstr = 
"select t.comp_id, t.field_id, t.p_id, t.value\n" +
"  from hris_dynamictable_value2 t\n" + 
" where t.comp_id = '"+comp_id+"'\n" + 
"   and t.p_id = '"+p_id+"'\n" + 
"   and t.field_id = '"+rs.getString(1)+"'and   seqno="+currentSEQ+
" and to_char(EFFECTIVEDATE,'dd/mm/yyyy') in to_char((select to_date('"+currentEFFDate+"','dd/mm/yyyy') from dual),'dd/mm/yyyy')";
st2 = conn.createStatement();
rs2 = st2.executeQuery(vstr);
String value = "";
while (rs2.next()) {
value += rs2.getString(4);
value += ",";
}
if(CR.isNNNE(value)){
value = value.substring(0,value.length()-1);
}
dBean.setDefault_value(value);

tempList.add(dBean);
}
}catch (SQLException e) {
e.printStackTrace();
}finally {
try {
st.close();
st2.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return tempList;
}
sql query results:

form表单域的动态设计的代码实现-customized tab information 模块代码开发总结(2)_第2张图片
dynamictableBean.java
this bean is mapping to dynamictable.

public class DynamictableBean {
private String comp_id;
private String field_id;    //the lable name
private String table_id;     //tableName
        ........
public String getComp_id() {
return comp_id;
}

public void setComp_id(String comp_id) {
this.comp_id = comp_id;
}
        ..........
}


get a dynamiclist base on the former code.and if it have exsited data .the defaultvalues have been setted. (dBean.setDefault_value(value)) ;
and then set this dynamiclist to request object.



how to generate the form fields on the jsp page?
hris_dynamictable_edit_htc.jsp


for (int next = 0; next <   dynamicList.size(); next++) {
                                 //dynamiclist get from request object.
DynamictableBean dBean = new DynamictableBean();
dBean = (DynamictableBean) dynamicList.get(next);
next_row = dBean.getValue_row();
<td width="25%" height="15"
colspan="<%=dBean.getLabel_column_span()%>"
<%if (dBean.getBgcolor() != null) {%> bgcolor="<%=dBean.getBgcolor()%>"
<%}%> align="<%=dBean.getAlignment()%>"
class="<%=dBean.getLabel_display_class()%>"><span
class="sbgH1"><FONT
class="<%=dBean.getValue_display_class()%>"> <%=dBean.getField_label()%></font>
<%
if (dBean.isNot_null()) {
%><span class="style1">*</span>
<%
}
%> </span></td>
<%
} else if ( dBean.getField_type() == 1) {
%>
<td width="25%" height="24"
colspan="<%=dBean.getLabel_column_span()%>"
<%if (dBean.getBgcolor() != null) {%> bgcolor="<%=dBean.getBgcolor()%>"
<%}%> align="<%=dBean.getAlignment()%>"
class="<%=dBean.getLabel_display_class()%>"><span
class="sbgH5">
<%
if (dBean.isNot_null()) {
%>*<%
}
%> <%=dBean.getField_label()%> </span></td>
<td width="25%" height="24"
colspan="<%=dBean.getValue_column_span()%>"
class="<%=dBean.getValue_display_class()%>">< input type=text
name="<%=dBean.getField_id()%>"
value="<%=dBean. getDefault_value()%>"
<%if (dBean.getComponent_length() != 0) {%>
size="<%=dBean.getComponent_length()%>" <%}%>
<%if (dBean.getTextbox_max_input() != 0) {%>
maxLength="<%=dBean.getTextbox_max_input()%>" <%}%>
<%if (!canEdit) {%> readonly="readonly" <%}%>></td>

the key code is on the top of current line.the related fields tags values are not just html code.they are all come from the dynmaiclist.
and the dynamiclist is the result of the sql query from the database.
like:dBean.getFiel_type()==1or.... will dynamic generate different input tags.
so we can get the dynamic fieds from database through the java code.
the advantages of this design:if the client or the requirement modified.we can just achieve the goal by modifying the database.


你可能感兴趣的:(动态表单)