2018年4 月月 发布的文章

js中return null和return false的区别

1. return返回null,起到中断方法执行的效果,只要不return false事件处理函数将会继续执行,表单将提交
2. return false,事件处理函数会取消事件,不再继续向下执行。比如表单将终止提交
这里面的return含有一些细节知识:
例如:onClick=’return add_onclick()’与 onClick=’add_onclick()’的区别
JAVASCRIPT在事件中调用函数时用return返回值实际上是对window.event.returnvalue进行设置。
而该值决定了当前操作是否继续。
当返回的是true时,将继续操作。
当返回是false时,将中断操作。
而直接执行时(不用return)。将不会对window.event.returnvalue进行设置
所以会默认地继续执行操作
在onclick事件上这样处理的

<input type="submit"  onclick="fun1();return false;" />

这样fun1既可以写自己想要的效果,return false 也可以取消默认的刷新行为

MyBatis获取刚刚插入对象的id

适用于对于支持自动生成主键的数据库
一、数据库表中ID的设计
id int型 自动递增
二、MyBatis的Mapper
useGeneratedKeys=”true” keyProperty=”id” 是重点

	<insert id="insert" useGeneratedKeys="true" keyProperty="id">
		INSERT INTO rdss2_report(
			report_name,
			componets,
			update_date,
			create_date,
			update_by,
			create_by
		) VALUES (
			#{reportName},
			#{componets},
			#{updateDate},
			#{createDate},
			#{updateBy.id},
			#{createBy.id}
		)
	</insert>

三、Java代码
重要代码如下,在保存完对象后,mybatis会自动把ID反填入对象中,直接获取即可

			rdss2ReportService.save(rdss2Report);
			System.out.println("test:"+rdss2Report.getId()+"-"+rdss2Report.getComponets());
	@RequestMapping(value="/saveReport", method=RequestMethod.POST)
	@ResponseBody
	public void saveReport(HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, Object> param) {
		JSONObject jo = new JSONObject();
		try {
			Rdss2Report rdss2Report = new Rdss2Report();
			rdss2Report.setReportName(param.get("title").toString());
			rdss2Report.setComponets(param.get("par").toString());
			rdss2ReportService.save(rdss2Report);
			System.out.println("test:"+rdss2Report.getId()+"-"+rdss2Report.getComponets());
			jo.put("result", "success");
			PrintWriter out = null;
			response.setContentType("application/json");
			out = response.getWriter();
			out.write(jo.toString());
		} catch (Exception e) {
			jo.put("result", "fail");
			PrintWriter out = null;
			response.setContentType("application/json");
			try {
				out = response.getWriter();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			out.write(jo.toString());
			e.printStackTrace();
		}
	}

Spring MVC前端JSON数据传到后端

传送List json
前端发送数据代码:

//准备数据
var componetsArray = new Array();
var dataTmp = {};
dataTmp["component_id"] = panel_biee_components.attr("id");
dataTmp["component_width"] = orgNumIndex;
componetsArray.push(JSON.stringify(dataTmp));
//发送请求
    $.ajax({
        type: "POST",
        url: $("#porjectPath").val()+"/f/report/rdss2Report/saveReport",
        datatype: "json",
        data:{"listParam" : componetsArray},
        success: function(data) {
            alert(data.result);
        }
    });

后端接收参数代码

	@RequestMapping(value="/saveReport", method=RequestMethod.POST)
	@ResponseBody
	public void saveReport(HttpServletRequest request, HttpServletResponse response,
			@RequestParam("listParam[]") List<String> param) {
		JSONObject jo = new JSONObject();
		try {
			System.out.println(param);
			jo.put("result", "success");
			PrintWriter out = null;
			response.setContentType("application/json");
			out = response.getWriter();
			out.write(jo.toString());
		} catch (Exception e) {
			jo.put("result", "fail");
			PrintWriter out = null;
			response.setContentType("application/json");
			try {
				out = response.getWriter();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			out.write(jo.toString());
			e.printStackTrace();
		}
	}

传送Map json复杂数据:
前端发送数据代码:

var componetsArray = new Array();
var dataTmp = {};
dataTmp["component_id"] = panel_biee_components.attr("id");
dataTmp["component_width"] = orgNumIndex;
componetsArray.push(JSON.stringify(dataTmp));
var jsonStr = JSON.stringify(componetsArray);
var jsonObject = {};
jsonObject['title']="标题";
jsonObject['par']=jsonStr;
    $.ajax({
        type: "POST",
        url: $("#porjectPath").val()+"/f/report/rdss2Report/saveReport",
        datatype: "json",
        data:jsonObject,
        success: function(data) {
            alert(data.result);
        }
    });

后端接收参数代码

	@RequestMapping(value="/saveReport", method=RequestMethod.POST)
	@ResponseBody
	public void saveReport(HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, Object> param) {
		JSONObject jo = new JSONObject();
		try {
			System.out.println(param);
			JSONArray ja = JSONArray.fromObject(param.get("par").toString());
			System.out.println(ja.toString());
			jo.put("result", "success");
			PrintWriter out = null;
			response.setContentType("application/json");
			out = response.getWriter();
			out.write(jo.toString());
		} catch (Exception e) {
			jo.put("result", "fail");
			PrintWriter out = null;
			response.setContentType("application/json");
			try {
				out = response.getWriter();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			out.write(jo.toString());
			e.printStackTrace();
		}
	}