window.open() POST submission of json data and background json serialization as objects

Keywords: JSON Front-end

When exporting, ajax implementation is not handled well. you can choose window.open(), poi or jxl as the background framework, or jxls template is the most convenient way to choose if it is not too complicated.

But when dealing with complex export reports, I chose to use poi framework to write and call poiAPI. window.open() passes a fixed parameter or a simple parameter using get, which is the default way. But if you need to pass complex json parameters, or jsonList parameters, and the parameters are uncertain, it is not easy to construct parameters with get method and it is likely to exceed the length range of url, you need to use post method to submit, for example, the parameters need to be passed are as follows, and the data of jsonList can be dynamically increased.

 

[{
		"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"]
	}
]



Direct up front-end window.open() code

         

{
                xtype : 'button',
                text: 'export',
                width: 100,
                style: 'margin-left:40px',
                handler: function(){
                    me.exportEventContent();
                }
            }
Export functions and submit them using form submit to simulate window.open()

    

    exportEventContent: function (){
        var me  = this;
        var url = _selfUrl; //URLs that need to be accessed
        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);
    }
When actually accessing the url, the parameters passed are as follows:

Background parsing parameter code

@ 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);
}

Posted by adamjones on Thu, 14 Feb 2019 17:15:19 -0800