How jsp and Java background data interact

<%
String path = request.getContextPath();

%>

Get the name of the project where the jsp is located

var ids = new Array();

$.ajax({
        type : "POST",
        contentType : 'application/json',
url : '<%=path%>/ui/product/havePsmPackage',
data:JSON.stringify(ids),
async :false,
dataType : "json",
success : function(data) {},error:function(){


}

});

1. If the frontend passes a collection, the backend can use the parameter @RequestBody List<String> ids to receive

2. If the foreground is this way of passing the value data:{"name":name,"id":id}, 

Then the background can receive by creating an entity class corresponding to the field name

Or use String name = request.getParameter("name") to receive

3. If the GET method selected by ajax, the field name of the background method and the input parameter name of the url are consistent to receive data.

4、window.location.href="<%=path%>/ui/psmpackage/toPsmPackageList";

Here's how to jump to a new page

@RequestMapping(value = "/toPsmPackageList")
public String toPsmPackageList(HttpServletRequest request) {
return "psmPackage/psmPackageList";

}

This is the acceptance method in the background, and the folder and file name corresponding to the jsp are returned.

The method of page jumping does not need the @ResponseBody annotation, and the method of obtaining the return value must be added, otherwise the return value cannot be obtained.

5. window.open(url); You can open a new page on the browser. The corresponding is window.close();

6. How to bring data to the newly added page

First: The first page passes

window.location.href="<%=path%>/ui/product/condition?lineCode="+lineCode+"&typeCode="+typeCode;传值

In the new page, you can get the value by var lineCode = '<%=(String)request.getParameter("lineCode")%>'

Second: the background code handles request.setAttribute("product", result);

<input name="id" type="text" value="${product.id}" />

The new page can directly pass the key value and use ${} to get the value.



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326659972&siteId=291194637