form表单回写技术java实现
程序员文章站
2024-03-11 18:12:19
本文实例为大家分享了form表单回写技术,供大家参考,具体内容如下
回写支持的java拼js的方法:
/**
* 回写表单
*
*...
本文实例为大家分享了form表单回写技术,供大家参考,具体内容如下
回写支持的java拼js的方法:
/** * 回写表单 * * @param mrequest * @return */ public static string writebackmaptoform(map mrequest) { return writebackmaptoform(mrequest, new string[]{}, "writebackmaptoform"); } /** * 回写表单 * * @param mrequest * @param ignorename 定义哪些key值的input不回写 * @return */ public static string writebackmaptoform(map mrequest, string[] ignorename, string jsfunctionname) { mrequest.remove("checkbox_template"); //不回写列表中checkbox的值 stringbuffer rtvalue = new stringbuffer(); rtvalue.append(" var mform = new object();\n"); rtvalue.append(" var indexarray = new array();\n"); rtvalue.append(" function writebackmaptoform() {\n"); iterator itmrequest = mrequest.keyset().iterator(); while (itmrequest.hasnext()) { string tempkey = (string) itmrequest.next(); object tempvalue = mrequest.get(tempkey); if (tempkey.startswith("venus") || tempkey.startswith("ranmin")) { continue; } if (rmstringhelper.arraycontainstring(ignorename, tempkey)) { continue; } string tempvaluenew = ""; if (tempvalue instanceof string) { //如果是单值,直接注入 tempvaluenew = rmstringhelper.replacestringtoscript((string)tempvalue); //从数据库中取出来以后需要转换1次 rtvalue.append(" indexarray[indexarray.length] = \"" + tempkey + "\";\n"); rtvalue.append(" mform[\"" + tempkey + "\"] = \"" + tempvaluenew + "\";\n"); } else if (tempvalue instanceof string[]) { //如果是多值,放入数组 rtvalue.append(" indexarray[indexarray.length] = \"" + tempkey + "\";\n"); string[] myarray = (string[]) tempvalue; if ( tempkey.equals("cmd") ){ tempvaluenew = rmstringhelper.replacestringtoscript(myarray[0]); rtvalue.append(" mform[\"" + tempkey + "\"] = \"" + tempvaluenew + "\";\n"); } else { rtvalue.append(" mform[\"" + tempkey + "\"] = ["); for (int i = 0; i < myarray.length; i++) { if (i > 0) rtvalue.append(","); tempvaluenew = rmstringhelper.replacestringtoscript(myarray[i]); rtvalue.append("\"" + tempvaluenew + "\""); } rtvalue.append("];\n"); } } else if (tempvalue instanceof timestamp) { //如果是时间戳,直接注入 if(tempvalue == null) { continue; } tempvaluenew = rmstringhelper.replacestringtoscript(tempvalue.tostring().substring(0,19)); rtvalue.append(" indexarray[indexarray.length] = \"" + tempkey + "\";\n"); rtvalue.append(" mform[\"" + tempkey + "\"] = \"" + tempvaluenew + "\";\n"); } else if (tempvalue instanceof bigdecimal){ tempvaluenew = rmstringhelper.replacestringtoscript(tempvalue.tostring()); rtvalue.append(" indexarray[indexarray.length] = \"" + tempkey + "\";\n"); rtvalue.append(" mform[\"" + tempkey + "\"] = \"" + tempvaluenew + "\";\n"); } else { if(tempvalue != null) { rmstringhelper.log("在回写页面时,遇到了未知java类型:" + tempvalue); } continue; } } rtvalue.append(" for(var i=0; i<indexarray.length; i++) {\n"); rtvalue.append(" writebackvalue(indexarray[i]);\n"); rtvalue.append(" }\n"); rtvalue.append(" }\n"); rtvalue.append(jsfunctionname + "();\n"); return rtvalue.tostring(); } //通过此方法将request中的值放入mform对象中 var mform = new object(); var indexarray = new array(); function writebackmaptoform() { indexarray[indexarray.length] = "att_id"; mform["att_id"] = ""; indexarray[indexarray.length] = "businesstypeoid"; mform["businesstypeoid"] = [""]; indexarray[indexarray.length] = "business_type1"; mform["business_type1"] = ""; indexarray[indexarray.length] = "business_type2"; mform["business_type2"] = "1"; indexarray[indexarray.length] = "cmd"; mform["cmd"] = "saveexaminerule"; indexarray[indexarray.length] = "document_content"; mform["document_content"] = "s2"; indexarray[indexarray.length] = "file_path"; mform["file_path"] = ""; indexarray[indexarray.length] = "file_template"; mform["file_template"] = ""; indexarray[indexarray.length] = "gxl"; mform["gxl"] = "null"; indexarray[indexarray.length] = "owner_id"; mform["owner_id"] = "s1"; for(var i=0; i<indexarray.length; i++) { writebackvalue(indexarray[i]); } } writebackmaptoform(); 关键语句jsp页面中加入后输出调用js方法: <script language="javascript"> <% //表单回写 if(request.getattribute(ruleexamineconstants.request_write_back_form_values) != null) { //如果request中取出的表单回写bean不为空 out.print(rmvohelper.writebackmaptoform((java.util.map)request.getattribute(ruleexamineconstants.request_write_back_form_values))); //输出表单回写方法的脚本 } map mapt = (java.util.map)request.getattribute(ruleexamineconstants.request_write_back_form_values); system.out.print("infois:"+mapt.entryset()); out.print("alert(1);"); %> </script> //上面语句实际上注入的js格式内容如: var mform = new object(); var indexarray = new array(); function writebackmaptoform() { indexarray[indexarray.length] = "_function_id_"; mform["_function_id_"] = "3670212500000000050"; indexarray[indexarray.length] = "cmd"; mform["cmd"] = "listbusinesstypepage"; for(var i=0; i<indexarray.length; i++) { writebackvalue(indexarray[i]); } } writebackmaptoform(); //注入后调用js回写表单方法 function writebackvalue(inputname) { if(form.elements[inputname] == undefined) { return false; } if(form.elements[inputname].value != undefined) { form.elements[inputname].value = mform[inputname]; } if(form.elements[inputname].length != undefined ) { var thisvalue = mform[inputname]; if(mform[inputname][0] == undefined) { thisvalue = new array(); thisvalue[thisvalue.length] = mform[inputname]; } if(form.elements[inputname].length != null) { var templength = form.elements[inputname].length; for(var j=0; j<templength; j++) { var thisobj = form.elements[inputname][j]; for(var k=0; k<thisvalue.length; k++) { if(thisobj.value == thisvalue[k]) { if( thisobj.checked != undefined) { thisobj.checked = true; break; } else if( thisobj.selected != undefined) { thisobj.selected = true; break; } } else { if( thisobj.checked != undefined) { thisobj.checked = false; } else if( thisobj.selected != undefined) { thisobj.selected = false; } } } } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助。