The response.getWriter().write() function is better than the return of springMvc

response.getWriter().write() can replace the return of springMvc (page, character) and can also return the picture can be received by ajax ( character), src (picture, full page character)

 

response.getWriter().write(str); This kind of writing is equivalent to generating jsp/html and returning html/jsp

 

The src of the tag can be received, and the page/image is written to the page (1, src loads the url, which can be returned by springmvc, and can be returned by response.getWriter().write(str))

 

The callback function of ajax can receive json data or html/jsp. There are two ways to write: 1, direct springmvc return (page/json), 2, response.getWriter().write(str) (page/json)

@responseBody (springmvc direct json), if the return is html, ajax needs to declare dataType: "html", without this return page, response.getWriter().write(str) can also return a string

That is to say, the things returned by the springMvc package can be received by src (page, json), and ajax can receive (page, json)

The most native response.getWriter().write(str) returns, src can receive (image, complete page characters), and ajax can also receive (string)

   //The entire jump page, not the page string in the form of string splicing, is returned with dataType='Html'

function checkChatRecord(type) {

setTotalMoney();

var conobjKey = $(".class_conobjKey").val();

var selectedCustomerKey = $(".class_RedFont span").html();

var ownerKey = $(".class_tbConobjCustomerKey").val();

if (selectedCustomerKey == undefined) {

selectedCustomerKey = $(".class_currentUserKey").val();

}

var urlCurrentUserKey= $(".class_currentUserKey").val();

if(type==1){

$("#chatRecord").html("<image src=\"" + webUrl + "/resources/images/icon/loader.gif\" />");

}

$.ajax({

url : webUrl + "/chat/offer/record/" + conobjKey + "/" + ownerKey

+ "/" + selectedCustomerKey + "/" + urlCurrentUserKey,

type : "get",

cache : false,

async : true,

success: function (ret) {

if($("#chatRecord").html().indexOf("<image src=")>0){$("#chatRecord").html("");};

if ($("#chatRecordJsonHidden").html() == ret) {

} else {

$("#chatRecordJsonHidden").html(ret);

writeChatRecord (ret);

}

},

error : function(retMsg) {

//$("#chatRecord").html("");

}

});

}

 

function writeChatRecord (ret) {

var json1 = (ret);

var json = eval(json1);

var str="";

$(".class_highPrice").html(json.highPrice);

$(".class_lowPrice").html(json.lowPrice);

for(i=0;i<json.chatRecord.length;i++){

var jsonchatRecord = json.chatRecord [i];

if(jsonchatRecord.chatChoice=="2"){

str+="<div class=\"mt20 p10\">";

str+="<div class=\"other1 fr\">";

str+="<div class=\"ico_jc1\"></div>";

str+="<div class=\"chat_name\">"+jsonchatRecord.chatName+"</div>";

str+="价格:<font class=\"cred fb\">"+jsonchatRecord.chatPrice+" </font>";

str+=jsonchatRecord.chatCurrency;

str+="/"+jsonchatRecord.chatNumUnit;

str+="; 数量:<font class=\"cred fb\">"+jsonchatRecord.chatNum+"</font>";

str + = jsonchatRecord.chatNumUnit;

str+="<p>"+jsonchatRecord.chatMsgText+"</p>";

str+="</div>";

str+="<div class=\"cb\"></div>";

str+="</div>";

}else{

str+="<div class=\"mt20 p10\">";

str+="<div class=\"other fl\">";

str+="<div class=\"chat_name1\">"+jsonchatRecord.chatName+"</div>";

str+="<div class=\"ico_jc\"></div>";

str+="价格:<font class=\"cred fb\">"+jsonchatRecord.chatPrice+" </font>";

str+=jsonchatRecord.chatCurrency;

str+="/"+jsonchatRecord.chatNumUnit;

str+="; 数量:<font class=\"cred fb\">"+jsonchatRecord.chatNum+"</font>";

str + = jsonchatRecord.chatNumUnit;

str+="<p>"+jsonchatRecord.chatMsgText+"</p>";

str+="</div>";

str+="<div class=\"cb\"></div>";

str+="</div>";

}

}

$("#chatRecord").html(str);

}

 

 

 

 

@RequestMapping(value = "/chat/offer/record/{conobjKey}/{ownerKey}/{tradeCustomerKey}/{urlCurrentUserKey}", method = RequestMethod.GET)

public void TbConOrdPriceGetRecord(HttpSession session, HttpServletRequest request, HttpServletResponse response, @PathVariable String conobjKey,

AthPathVariable String ownerKey, athPathVariable String tradeCustomerKey, athPathVariable String urlCurrentUserKey, Model model)

throws EsteelException, IOException {

 

if (conobjKey.equals("") || ownerKey.equals("") || tradeCustomerKey.equals("") || urlCurrentUserKey.equals("")) {

writeToResponse(response, "invalid key");

} else {

/* Get the key of the current user */

String currentuserKey = getCurrentUserKey(request, session);

if (!currentuserKey.equals(urlCurrentUserKey)) {

currentuserKey = urlCurrentUserKey;

}

if (!currentuserKey.equals(ownerKey) && !currentuserKey.equals(tradeCustomerKey)) {

/* If the current user is not the owner or client, view is not allowed */

writeToResponse(response, "access denied");(///Other roles cannot be read)

} else {

TbConObj tbConObj = tbConObjService.getTbConObjById(conobjKey);//-- table name: TB_CON_OBJ-- table description: offer subject

List<TbConOrdVo> tbConOrdVoList = tbConOrdService.getTbConOrdVoList(conobjKey, tradeCustomerKey); ////TB_CON_ORD, offer order (offer)

String strJson = "({";

strJson += "\"chatRecord\":[";

int k = 0;

String highPrice = "1999999999";

String lowPrice = "0";

for (TbConOrdVo tbConOrdVo1 : tbConOrdVoList) {

/* Loop to get the highest and lowest prices */

if (tbConOrdVo1.getOrdpriceMan() != null) {

if (tbConOrdVo1.getOrdpriceMan().equals("B")) {///B is the initiator, B is the buyer

/* is the initiator */

if (tbConOrdVo1.getContradeType().equals("A")) {

/* is a sell order */

if (Double.parseDouble(highPrice) > Double.parseDouble(tbConOrdVo1.getOrderPrice())) {

highPrice = tbConOrdVo1.getOrderPrice();

}

} else {

/* is a buy order */

if (Double.parseDouble(lowPrice) < Double.parseDouble(tbConOrdVo1.getOrderPrice())) {

lowPrice = tbConOrdVo1.getOrderPrice();

}

}

} else {

/* Not the initiator */

if (tbConOrdVo1.getContradeType().equals("A")) {

/* is a sell order */

if (Double.parseDouble(lowPrice) < Double.parseDouble(tbConOrdVo1.getOrderPrice())) {

lowPrice = tbConOrdVo1.getOrderPrice();

}

} else {

/* is a buy order */

if (Double.parseDouble(highPrice) > Double.parseDouble(tbConOrdVo1.getOrderPrice())) {

highPrice = tbConOrdVo1.getOrderPrice();

}

}

}

} else {

/* Not the initiator */

if (tbConOrdVo1.getContradeType().equals("A")) {

/* is a sell order */

if (Double.parseDouble(lowPrice) < Double.parseDouble(tbConOrdVo1.getOrderPrice())) {

lowPrice = tbConOrdVo1.getOrderPrice();

}

} else {

/* is a buy order */

if (Double.parseDouble(highPrice) > Double.parseDouble(tbConOrdVo1.getOrderPrice())) {

highPrice = tbConOrdVo1.getOrderPrice();

}

}

}

k = k + 1;

strJson += "{";

String msgText = tbConOrdVo1.getMsgText();

if (msgText == null || msgText.contains("Please enter your negotiation message, the maximum length is 300 characters! Press Ctrl+Enter to submit!")) {

msgText = "";

}

/* replace the newline character, otherwise an error will be reported */

msgText = msgText.replaceAll("\n", "");

int chatChoice = 0;

if (tbConOrdVo1.getOrdpriceMan() != null) {

if (tbConOrdVo1.getOrdpriceMan().equals("B")) {

/* is the initiator */

chatChoice = 1;

strJson += "\"chatChoice\":\"1\",";

} else {

chatChoice = 2;

/* Not the initiator */

strJson += "\"chatChoice\":\"2\",";

}

} else {

chatChoice = 2;

strJson += "\"chatChoice\":\"2\",";

}

if (tbConObj.getAnonymMark().equals("B")) {

strJson += "\"chatName\":\"***\",";

} else {

/*

* 当currentuserKey eq

* tbConObj.getCustomerKey(), indicating that the owner is currently browsing

*/

/*

* 当currentuserKey eq

* tbConOrdVo1.getCustomerKey(), indicating that the guest is browsing

*/

if (chatChoice == 1 && currentuserKey.equals(tbConObj.getCustomerKey())) {

strJson += "\"chatName\":\"我\",";

} else if (chatChoice == 2 && currentuserKey.equals(tbConOrdVo1.getCustomerKey())) {

strJson += "\"chatName\":\"我\",";

} else {

strJson += "\"chatName\":\"" + tbConOrdVo1.getCustomerName() + "\",";

}

}

strJson += "\"chatPrice\":\"" + tbConOrdVo1.getOrderPrice() + "\",";

if (tbConObj.getCurrencyType().equals("A")) {

strJson += "\"chatCurrency\":\"元\",";

} else if (tbConObj.getCurrencyType().equals("B")) {

strJson += "\"chatCurrency\":\"美元\",";

}

strJson += "\"chatNumUnit\":\"" + tbConObj.getNumUnit() + "\",";

strJson += "\"chatNum\":\"" + tbConOrdVo1.getOrderNum() + "\",";

strJson += "\"chatMsgText\":\"" + msgText + "\"}";

if (k < tbConOrdVoList.size()) {

strJson += ",";

}

}

strJson += "],";

strJson += "\"highPrice\":\"" + highPrice + "\",";

strJson += "\"lowPrice\":\"" + lowPrice + "\"";

strJson += "})";

writeToResponse(response, strJson);//Assemble to the page

}

}

}

private void writeToResponse(HttpServletResponse response, String str) throws IOException {

response.setCharacterEncoding("UTF-8");

response.getWriter().write(str);

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326490179&siteId=291194637