当前位置: 首页 > 编程笔记 >

form表单回写技术java实现

堵毅然
2023-03-14
本文向大家介绍form表单回写技术java实现,包括了form表单回写技术java实现的使用技巧和注意事项,需要的朋友参考一下

本文实例为大家分享了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;                
            }  
          }
        }
      }
    }  
           
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助。

 类似资料:
  • 问题内容: 我正在寻找 最雅致的 方式来表单化(使用jQuery)。 你怎么做到这一点? 问题答案: 这是我的解决方案(我认为这是一个渐进式增强解决方案),仅使用jQuery而没有任何插件: 更新: 如果您的POST响应是“带有表单的HTML”,请尝试以下操作:

  • 本文向大家介绍Form表单按回车自动提交表单的实现方法,包括了Form表单按回车自动提交表单的实现方法的使用技巧和注意事项,需要的朋友参考一下 1.form表单中只有一个input标签,按回车键将自动提交表单 当form表单中只有一个<input type="text" name='name' />时按回车键将会自动将表单提交。 如果不想让其自动提交可以这样做: 再添加一个<input type=

  • 介绍 用于数据录入、校验,支持输入框、单选框、复选框、文件上传等类型,2.5 版本开始支持此组件。 引入 import { createApp } from 'vue'; import { Form } from 'vant'; const app = createApp(); app.use(Form); 代码演示 基础用法 在表单中,每个 Field 组件 代表一个表单项,使用 Field

  • 基本表单使用 <form id="my-form" action="/test"> <div class="field-row"> <label class="field-label">姓名:</label> <span data-ui-id="uNameTextBox"><input type="text"></span> </div> <div class

  • 表单组件。 Usage 全部引入 import { Form } from 'beeshell'; 按需引入 import { Form } from 'beeshell/dist/components/Form'; Examples Standard 布局设计 一个 Form.Item 内部分成三个区域,label 放在左边,Form.Item 的 children 的第一个元素放在右边,其

  • form 表单 效果展示 基本用法 <form class="page-wrap" method="post" action="./doc/json/ajaxDone.json" onsubmit="return validateCallback(this, navViewAjaxDoneClose)" > <header> <div class="toolbar">

  • Form 表单 1.3.0 平台差异说明 App H5 微信小程序 支付宝小程序 百度小程序 头条小程序 QQ小程序 √ √ √ √ √ √ √ 基本使用 此组件一般是用于表单验证使用,每一个表单域由一个u-form-item组成,表单域中可以放置u-input、u-checkbox、u-radio、u-switch等。 在表单组中,通过model参数绑定一个对象,这个对象的属性为各个u-form

  • 由输入框、选择器、单选框、多选框等控件组成,用以收集、校验、提交数据 典型表单 包括各种表单项,比如输入框、选择器、开关、单选框、多选框等。 在 Form 组件中,每一个表单域由一个 Form-Item 组件构成,表单域中可以放置各种类型的表单控件,包括 Input、Select、Checkbox、Radio、Switch、DatePicker、TimePicker <el-form ref="f