response.getWriter().write()功能优于springMvc的返回

response.getWriter().write()可以替代springMvc的返回(页面,字符)而且还可返回图片都可被ajax(字符),src(图片,完整页面字符)接收

response.getWriter().write(str);这种写入的就相当于生成了jsp/html,返回html/jsp

标签的src可以接收,页面/图片 写的方式页面(1,src加载url,这是可以springmvc方式返回,可以response.getWriter().write(str)返回)

ajax的回调函数可以接收json数据,也可以接收html/jsp,写的方式有两种1,直接springmvc返回(页面/json),2,response.getWriter().write(str)(页面/json)

@responseBody(springmvc直接json),如果返回的是html,ajax需声明dataType:"html",不加这个返回页面,用response.getWriter().write(str)也可返回字符串

也就是说springMvc封装返回的东西src可以接收(页面,json),ajax可以接收(页面,json)

最原生的response.getWriter().write(str)返回,src可以接收(图片,完整的页面字符),ajax也可接收(字符串)

   //整个跳转页面,不是字符串拼接形式的页面字符串返回,用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,

@PathVariable String ownerKey, @PathVariable String tradeCustomerKey, @PathVariable String urlCurrentUserKey, Model model)

throws EsteelException, IOException {

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

writeToResponse(response, "invalid key");

} else {

/* 获取当前用户的key */

String currentuserKey = getCurrentUserKey(request, session);

if (!currentuserKey.equals(urlCurrentUserKey)) {

currentuserKey = urlCurrentUserKey;

}

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

/* 如果当前用户不是主人或者客户,不允许查看 */

writeToResponse(response, "access denied");(///其他角色不可以看)

} else {

TbConObj tbConObj = tbConObjService.getTbConObjById(conobjKey);//-- 表名称: TB_CON_OBJ-- 表描述: 报盘标的

List<TbConOrdVo> tbConOrdVoList = tbConOrdService.getTbConOrdVoList(conobjKey, tradeCustomerKey); ////TB_CON_ORD,报盘委托(报盘)

String strJson = "({";

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

int k = 0;

String highPrice = "1999999999";

String lowPrice = "0";

for (TbConOrdVo tbConOrdVo1 : tbConOrdVoList) {

/* 循环得到最高价和最低价 */

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

if (tbConOrdVo1.getOrdpriceMan().equals("B")) {///B是发起方,B是买方

/* 是发起方 */

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

/* 是卖单 */

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

highPrice = tbConOrdVo1.getOrderPrice();

}

} else {

/* 是买单 */

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

lowPrice = tbConOrdVo1.getOrderPrice();

}

}

} else {

/* 不是是发起方 */

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

/* 是卖单 */

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

lowPrice = tbConOrdVo1.getOrderPrice();

}

} else {

/* 是买单 */

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

highPrice = tbConOrdVo1.getOrderPrice();

}

}

}

} else {

/* 不是是发起方 */

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

/* 是卖单 */

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

lowPrice = tbConOrdVo1.getOrderPrice();

}

} else {

/* 是买单 */

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

highPrice = tbConOrdVo1.getOrderPrice();

}

}

}

k = k + 1;

strJson += "{";

String msgText = tbConOrdVo1.getMsgText();

if (msgText == null || msgText.contains("请录入您的议价留言,最大为300个字符!按Ctrl+Enter提交!")) {

msgText = "";

}

/* 替换掉换行符,否则会报错 */

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

int chatChoice = 0;

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

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

/* 是发起方 */

chatChoice = 1;

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

} else {

chatChoice = 2;

/* 不是是发起方 */

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

}

} else {

chatChoice = 2;

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

}

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

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

} else {

/*

* 当currentuserKey eq

* tbConObj.getCustomerKey(),说明当前是主人在浏览

*/

/*

* 当currentuserKey eq

* tbConOrdVo1.getCustomerKey(),说明是客人在浏览

*/

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);//组装到页面

}

}

}

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

response.setCharacterEncoding("UTF-8");

response.getWriter().write(str);

}

猜你喜欢

转载自yuhuiblog6338999322098842.iteye.com/blog/2317156