京寵展信息指南
995
2025-03-31
本文目錄一覽:
在進行網站建設的過程中,動態表單主要負責數據采集的功能,比如可以采集訪問者的名字和e-mail地址、調查表、留言簿等等。
一個表單有三個基本組成部分:
表單標簽:這里面包含了處理表單數據所用CGI程序的URL以及數據提交到服務器的方法。
表單域:包含了文本框、密碼框、隱藏域、多行文本框、復選框、單選框、下拉選擇框和文件上傳框等。
表單按鈕:包括提交按鈕、復位按鈕和一般按鈕;用于將數據傳送到服務器上的CGI腳本或者取消輸入,還可以用表單按鈕來控制其他定義了處理腳本的處理工作。
應用場景
項目中往往需要動態的創建一個表單 或者添加一個新的數據模板 這時候因為需要在運行時動態的創建表以及動態的維護表字段甚至表關系 使得普通java解決方案變得困難重重
實現工具
Hibernate + Spring + Groovy +Freemarker
Hibernate 作用很簡單負責創建數據庫表這樣可以避免我們自己去寫復雜的sql和判斷
Spring 作為橋梁起到連接紐帶的作用
Groovy做為動態語言 在項目運行時根據模板創建訪問數據庫 或者控制層代碼
Freamker 可以根據提前定義好的模板生成 hibernate配置文件 以及Groovy代碼
實現原理
首先創建Form 和 FromAttribute 兩張表關系一對多 Form表記錄表單的名稱 類別 甚至是作為在動態生成表單時的css樣式信息 FromAttribute記錄表單字段信息 如名稱 類別等 有動態表單了表單以及表單項的信息后就可以創建數據庫表了
測試代碼
public void testGenerator(){
Form form = formService getAll() get( )
List list = formAttributeService
getAttributeListByFormId(form getId()) form setFormAttributeList(list) DbGenerator dg = new DbGenerator(form dataSource) dg generator()
}
DbGenerator
import java io IOException import java io StringWriter import java io Writer import java sql SQLException import java util HashMap import java util Map import java util Properties
import javax sql DataSource
import hibernate tool hbm ddl SchemaExport import slf j Logger import slf j LoggerFactory
import freemarker template Configuration import freemarker template Template import freemarker template TemplateException
public class DbGenerator {
private DataSource dataSource
protected Map root = new HashMap()
private static Logger log = LoggerFactory getLogger(FormGenerator class)
protected String path
protected String packageName
private Form form
protected Configuration getConfig(String resource){
Configuration cfg = new Configuration()
cfg setDefaultEncoding( UTF ) cfg setClassForTemplateLoading(this getClass() resource)
return cfg
}
public DbGenerator(Form form DataSource dataSource) { this form = form this dataSource = dataSource
}
public void generator(){
if(null == form getFormAttributeList() || form getFormAttributeList() size() == ){
return
}
Template t
try {
t = getConfig( /template ) getTemplate( hibernate ftl )
Writer out = new StringWriter()
t process(getMapContext() out) String xml = out toString()
createTable(xml)
log debug(xml)
} catch(IOException e){
e printStackTrace()
} catch(TemplateException e){
e printStackTrace()
}
@SuppressWarnings( unchecked )
Map getMapContext(){
root put( entity form)
return root
}
public void createTable(String xml){
hibernate cfg Configuration conf = new hibernate cfg Configuration() nfigure( /hibernate/hibernate cfg xml )
Properties extraProperties = new Properties()
extraProperties put( hibernate hbm ddl auto create ) conf addProperties(extraProperties)
conf addXML(xml)
SchemaExport dbExport
try {
dbExport = new SchemaExport(conf dataSource getConnection()) // dbExport setOutputFile(path) dbExport create(false true)
} catch(SQLException e){
// TODO Auto generated catch block
e printStackTrace()
}
}
class hibernateGenerator {
}hibernate ftl
hibernate cfg xml
hibernate dialect SQLServerDialect
net sourcefe jtds jdbc Driver
jdbc jtds sqlserver:// databasename=struts SelectMethod=cursor
sa
sa
true
update
——
創建好數據庫后就要利用groovy動態創建訪問代碼了 先看測試代碼再看具體實現
public void testGroovy(){
Form form = formService get( )
List list = formAttributeService
getAttributeListByFormId(form getId()) form setFormAttributeList(list)
FormGenerator fg = new FormGenerator(form)
String groovycode = fg generator() ClassLoader parent = getClass() getClassLoader()
GroovyClassLoader loader = new GroovyClassLoader(parent)
Class groovyClass = loader parseClass(groovycode)
GroovyObject groovyObject = null
try {
groovyObject = (GroovyObject) groovyClass newInstance()
} catch(InstantiationException e){
e printStackTrace()
} catch(IllegalAccessException e){
e printStackTrace()
}
// map中key為formAttribute中描述該表單字段在數據庫中的名稱c_columnName
//具體情況根據formAttribute而定
Map map = new HashMap()
map put( name limq )
//調用insert方法插入數據
int c = (Integer) groovyObject invokeMethod( insert map)
//調用getAll方法獲得所有動態表中的數據
Object o = groovyObject invokeMethod( getAll null)
List list =(List)o
Object obj = list get( )
try {
String tname = (String) BeanUtils getDeclaredProperty(obj name ) System out println(tname)
} catch(IllegalAccessException e){
e printStackTrace()
} catch(NoSuchFieldException e){
e printStackTrace()
}
//調用search方法查詢動態表
List returnList = (List) groovyObject invokeMethod( search map)
for(Map map returnList){
//同理此處根據FromAttribute而定
System out println(map get( id )) System out println(map get( name )) System out println(map get( type ))
}
}FormGenerator 創建訪問數據庫Groovy代碼
public class FormGenerator {
protected Map root = new HashMap()
private static Logger log = LoggerFactory getLogger(FormGenerator class)
protected String path
protected String packageName
private Form form
protected Configuration getConfig(String resource){
Configuration cfg = new Configuration()
cfg setDefaultEncoding( UTF ) cfg setClassForTemplateLoading(this getClass() resource)
return cfg
}
public FormGenerator(Form form){
this form = form
}
public String generator(){
String returnstr = null
Template t
try {
t = getConfig( /template ) getTemplate( FormService ftl ) //Writer out = new OutputStreamWriter(new FileOutputStream(new File(path)) UTF )
Writer out = new StringWriter()
t process(getMapContext() out) returnstr = out toString() log debug(returnstr)
} catch(IOException e){
e printStackTrace()
} catch(TemplateException e){
e printStackTrace()
}
return returnstr
}
@SuppressWarnings( unchecked )
Map getMapContext(){
root put( entity form) root put( insert SqlHelper buildInsertStatement(form)) root put( update SqlHelper buildUpdateStatement(form))
root put( insertParameter SqlHelper buildInsertparameter(form)) root put( updateParameter SqlHelper buildUpdateparameter(form))
root put( delete SqlHelper buildDeleteStatement(form)) root put( query ? SqlHelper buildQueryStatement(form))
return root
}
}FormService ftl import java sql ResultSet import java sql SQLException import java sql Types import sprire RowMapper import sprire RowMapperResultSetExtractor import re dao DataSourceFactory import mons lang builder ToStringBuilder import mons lang builder ToStringStyle
class ${entity name動態表單?cap_first}Dao {
def insert = ${insert}
def delete = ${delete}
def update = ${update}
def int insert(entity){
def Object[] params = [${insertParameter}]
def int[] types=[Types VARCHAR ] return DataSourceFactory getJdbcTemplate() update(insert params types)
}
def int update(entity){
def Object[] params = [${updateParameter}]
return DataSourceFactory getJdbcTemplate() update(update params)
}
def int delete(String entityId){
def Object[] params =[entityId]
return DataSourceFactory getJdbcTemplate() update(delete params)
}
def search(entity){
${query}
println(query)
return DataSourceFactory getJdbcTemplate() queryForList(query)
}
}
lishixinzhi/Article/program/Java/hx/201311/25599
1、表單繪制:MyApps開發平臺采用類似Word式的編輯頁面,可借助表格控件自主配置,也可直接從Word或Excel中一鍵復制粘貼,另外左右托拉拽式和源碼編輯模式也可以試試;
2、表單控件:平臺自帶了30種功能控件,拖拉拽就能快速調用;如果實在沒有自己想要的控件,也支持自定義拓展并自由復用;
3、操作按鈕:平臺自帶25種表單操作按鈕,同樣支持操作按鈕自定義拓展;
4、表單規則:對字段提供固定值、多候選項、聯動控制、接口讀寫和腳本計算等5種方式實現復雜業務邏輯的配置;對字段值的計算、權限、校驗、隱藏、只讀和打印提供配置化設置,操作按鈕在動作執行前、執行時和執行后均可觸發關聯業務操作;
5、表單發布:Myapps開發平臺采用熱發布模式,后臺保存前臺刷新即生效,不需要像傳統的軟件開發那樣做打包編譯動作!表單配置信息都存儲在XML文件中,通過復制粘貼即可輕松實現版本管理。
MyApps快速開發平臺動態表單
關于動態表單和動態表單是什么意思的介紹到此就結束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關注本站。 動態表單的介紹就聊到這里吧,感謝你花時間閱讀本站內容,更多關于動態表單是什么意思、動態表單的信息別忘了在本站進行查找喔。版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。