1. 程式人生 > >window.open() POST 方式提交json資料,以及後臺的json序列化為物件

window.open() POST 方式提交json資料,以及後臺的json序列化為物件

在匯出時候,ajax實現並不好處理,可以選擇用window.open()的方式,後臺框架選擇poi或者jxl即可,若不是太過複雜的匯出選擇jxls模板的方式最為方便。

但在處理複雜的匯出報表時候我選擇是用poi框架寫呼叫poiAPI的方式實現。window.open()傳遞固定引數或者簡單引數時候使用get即預設的方式即可。但若是需要傳遞比較複雜的json引數,或者是jsonList的引數,並且引數不定時候,用get方式並不好構造引數而且很可能會超出url的長度範圍,需要使用post的方式去提交,例如需要傳遞的引數如下,並且jsonList的資料可以動態增加

[{
		"fieldFormat" : "STRING",
		"operator" : "IS_NOT",
		"specialField" : "status",
		"values" : ["DRAFT"]
	}, {
		"operator" : "LESS_THAN",
		"fieldFormat" : "DATE",
		"specialField" : "happened_time",
		"values" : ["2017-02-21T00:00:00"]
	}, {
		"operator" : "LESS_THAN_EQUAL",
		"fieldFormat" : "DATE",
		"specialField" : "opinion_time",
		"values" : ["2017-02-21T23:59:59"]
	}, {
		"specialField" : "confirm_time",
		"fieldFormat" : "DATE",
		"values" : ["2017-02-21T23:59:59"],
		"operator" : "LESS_THAN_EQUAL"
	}, {
		"operator" : "GREATER_THAN_EQUAL",
		"fieldFormat" : "DATE",
		"specialField" : "confirm_time",
		"values" : ["2017-02-14T00:00:00"]
	}, {
		"operator" : "IS",
		"fieldFormat" : "STRING",
		"specialField" : "status",
		"values" : ["SUBMIT"]
	}
]

         直接上前端window.open()程式碼

{
                xtype : 'button',
                text: '匯出',
                width: 100,
                style: 'margin-left:40px',
                handler: function(){
                    me.exportEventContent();
                }
            }
       匯出函式,使用form的submit模擬window.open()方式提交
    exportEventContent: function (){
        var me  = this;
        var url = _selfUrl; //需要去進行訪問的url
        var predictions = me.getSearchPredicate();
        me.openNewPost('POST',url,predictions,"_blank");
    }
    // Arguments :
    //  verb : 'GET'|'POST'
    //  target : an optional opening target (a name, or "_blank"), defaults to "_self"
    openNewPost: function(verb, url, data, target) {
        var form = document.createElement("form");
        form.action = url;
        form.method = verb;
        form.target = target || "_self";
        if (data) {
            for (var key in data) {
                var input = document.createElement("textarea");
                input.name = key;
                input.value = typeof data[key] === "object" ? JSON.stringify(data[key]) : data[key];
                form.appendChild(input);
            }
        }
        form.style.display = 'none';
        form.enctype='application/json';
        document.body.appendChild(form);
        form.submit();
        document.body.removeChild(form);
    }
      實際去訪問url時,傳遞的引數如下:

      後臺解析傳參程式碼

@ RequestMapping( value = "/exportExcel" )
public void exportExcel( HttpServletRequest request, HttpServletResponse response ) throws ParseException, IOException
{
	List < ObjectTmp > objectTmps= jsonStrToBeanList( request );
}


public static List < ObjectTmp > jsonStrToBeanList( HttpServletRequest request ) throws ParseException
{
	List < ObjectTmp >	resultList	= new ArrayList < ObjectTmp > ();
	int			i		= 0;
	while ( request.getParameter( i + "" ) != null )
	{
		JSONObject	jsonObject	= JSONObject.fromObject( request.getParameter( i + "" ) );
		ObjectTmp	tmp		= (ObjectTmp) JSONObject.toBean( jsonObject, ObjectTmp.class );
		resultList.add( tmp );
		i++;
	}

	return(resultList);
}