读取HTML页面form信息并生成pojo的小工具
<a style="width:190px;" onclick="createPojo()" ><img src="images/ponderui/icon/execute.png" alt=""/>生成pojo</a>
var pForms; function createPojo(){ var forms = document.forms; if(forms.length>0){ pForms = forms; window.open("<%=request.getContextPath()%>/createPojoJavaCode.html"); }else{ alert("该页面没有form!"); } Event.stop(event); }
弹出的HTML页面代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- Don't modify the document!!! --> <HTML xmlns:ctais> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUIV="author" CONTENT="zhanggx"> <TITLE>生成pojo</TITLE> </HEAD> <BODY> <div> <h2>代码</h2> <textarea id="textArea" rows="10" cols="100"></textarea> </div> </BODY> </HTML>
<script type="text/javascript"> String.prototype.trim=function() { return this.replace(/(^\s*)|(\s*$)/g,''); }; var forms = window.opener.pForms; if(forms){ var fragment = document.createDocumentFragment(); for(var i=0; i<forms.length; i++){ var table = document.createElement("table"); //("<table id='table" + i +"' width='100%' border='1' style='border-color:#000000;border-collapse:collapse'></table>"); table.id = "table"+i; table.width = "100%"; table.border = "1"; table.style.borderColor = "#000000"; table.style.borderCollapse = "collapse"; var trIndex = 1; var tbody = document.createElement("tbody"); table.appendChild(tbody); tbody.insertRow(0); tbody.rows[0].insertCell(0); tbody.rows[0].cells[0].appendChild(document.createTextNode("字段lable(供参考)")); tbody.rows[0].insertCell(1); tbody.rows[0].cells[1].appendChild(document.createTextNode("字段type")); tbody.rows[0].insertCell(2); tbody.rows[0].cells[2].appendChild(document.createTextNode("字段name")); tbody.rows[0].insertCell(3); tbody.rows[0].cells[3].appendChild(document.createTextNode("操作")); var inputs = forms[i].getElementsByTagName("input"); var selects = forms[i].getElementsByTagName("select"); var textares = forms[i].getElementsByTagName("textarea"); for(var j=0; j<inputs.length; j++){ var ele = inputs[j]; if(ele.name){ var type = inputs[j].type || "text"; type = type.toLowerCase(); var lableVal = ""; var dataType = "String"; if(type=="text"){ var inputTableCells = ele.parentNode.parentNode.cells; if(inputTableCells){ for(var x=0; x<inputTableCells.length; x++){ if(inputTableCells[x] == ele.parentNode){ var lableTd = inputTableCells[x-2]; var textNode = lableTd.childNodes[0]; if(textNode.nodeType==3 && textNode.nodeValue.trim()!=""){ lableVal = textNode.nodeValue; } var nextNode = textNode.nextSibling; if(nextNode && nextNode.nodeType==3 && nextNode.nodeValue.trim()!=""){ lableVal += nextNode.nodeValue; } } } } var mode = ele.getAttribute("mode"); if(mode){ if(mode=="date"){ dataType = "java.sql.Date"; }else if(mode == "datetime"){ dataType = "java.sql.Timestamp"; }else if(mode == "int"){ dataType = "int"; }else{ dataType = "java.math.BigDecimal"; } } } var trObj = tbody.insertRow(-1); trObj.insertCell(0); var lableInput = document.createElement("input"); lableInput.name = "inputLable"; lableInput.value = lableVal; trObj.cells[0].appendChild(lableInput); trObj.insertCell(1); var typeInput = document.createElement("input"); typeInput.name = "inputType"; typeInput.value = dataType; trObj.cells[1].appendChild(typeInput); trObj.insertCell(2); var nameInput = document.createElement("input"); nameInput.name = "inputName"; nameInput.value = ele.name; trObj.cells[2].appendChild(nameInput); trObj.insertCell(3); var a = document.createElement("a"); a.href = "javascript:deleteRow("+ (trIndex++) +","+ i +")"; a.appendChild(document.createTextNode("删除")); trObj.cells[3].appendChild(a); } } for(var j=0; j<selects.length; j++){ var ele = selects[j]; if(ele.name){ var lableVal = ""; var dataType = "String"; var inputTableCells = ele.parentNode.parentNode.cells; for(var x=0; x<inputTableCells.length; x++){ if(inputTableCells[x] == ele.parentNode){ var lableTd = inputTableCells[x-2]; var textNode = lableTd.childNodes[0]; if(textNode.nodeType==3 && textNode.nodeValue.trim()!=""){ lableVal = textNode.nodeValue; } var nextNode = textNode.nextSibling; if(nextNode && nextNode.nodeType==3 && nextNode.nodeValue.trim()!=""){ lableVal += nextNode.nodeValue; } } } var trObj = tbody.insertRow(-1); trObj.insertCell(0); var lableInput = document.createElement("input"); lableInput.name = "inputLable"; lableInput.value = lableVal; trObj.cells[0].appendChild(lableInput); trObj.insertCell(1); var typeInput = document.createElement("input"); typeInput.name = "inputType"; typeInput.value = dataType; trObj.cells[1].appendChild(typeInput); trObj.insertCell(2); var nameInput = document.createElement("input"); nameInput.name = "inputName"; nameInput.value = ele.name; trObj.cells[2].appendChild(nameInput); trObj.insertCell(3); var a = document.createElement("a"); a.href = "javascript:deleteRow("+ (trIndex++) +","+ i +")"; a.appendChild(document.createTextNode("删除")); trObj.cells[3].appendChild(a); } } for(var j=0; j<textares.length; j++){ var ele = textares[j]; if(ele.name){ var lableVal = ""; var dataType = "String"; var inputTableCells = ele.parentNode.parentNode.cells; for(var x=0; x<inputTableCells.length; x++){ if(inputTableCells[x] == ele.parentNode){ var lableTd = inputTableCells[x-2]; var textNode = lableTd.childNodes[0]; if(textNode.nodeType==3 && textNode.nodeValue.trim()!=""){ lableVal = textNode.nodeValue; } var nextNode = textNode.nextSibling; if(nextNode && nextNode.nodeType==3 && nextNode.nodeValue.trim()!=""){ lableVal += nextNode.nodeValue; } } } var trObj = tbody.insertRow(-1); trObj.insertCell(0); var lableInput = document.createElement("input"); lableInput.name = "inputLable"; lableInput.value = lableVal; trObj.cells[0].appendChild(lableInput); trObj.insertCell(1); var typeInput = document.createElement("input"); typeInput.name = "inputType"; typeInput.value = dataType; trObj.cells[1].appendChild(typeInput); trObj.insertCell(2); var nameInput = document.createElement("input"); nameInput.name = "inputName"; nameInput.value = ele.name; trObj.cells[2].appendChild(nameInput); trObj.insertCell(3); var a = document.createElement("a"); a.href = "javascript:deleteRow("+ (trIndex++) +","+ i +")"; a.appendChild(document.createTextNode("删除")); trObj.cells[3].appendChild(a); } } var trObj = tbody.insertRow(); var tdObj = trObj.insertCell(); tdObj.colspan = "4"; tdObj.align = "left"; var bt = document.createElement("button"); bt.onclick = executeCode; bt.appendChild(document.createTextNode("生成pojo")) tdObj.appendChild(bt); fragment.appendChild(table); } document.body.appendChild(fragment); document.writeln("<br>"); } function deleteRow(trIndex,tableIndex){ if(window.confirm("确认删除此行?")){ document.getElementById("table"+tableIndex).deleteRow(trIndex); } } function executeCode(){ var table = document.getElementById(event.srcElement.parentNode.parentNode.parentNode.parentNode.id); var values = table.getElementsByTagName("input"); var pojoText = ""; var index = 0; for(var i=0; i<(values.length/3); i++){ pojoText += "//"; pojoText += values[index++].value; pojoText += "\r"; pojoText += "private "; pojoText += values[index++].value; pojoText += " "; pojoText += values[index++].value; pojoText += ";\r"; } var childNode = document.getElementById("textArea").childNodes[0]; var newNode = document.createTextNode(pojoText); if(childNode){ document.getElementById("textArea").replaceChild(newNode,childNode); }else{ document.getElementById("textArea").appendChild(newNode); } } </script>
效果图: