`
y806839048
  • 浏览: 1108335 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

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

}

 

分享到:
评论

相关推荐

    response.getWriter().write()用于ajax

    总的来说,`response.getWriter().write()`是Java Web开发中处理AJAX请求的关键部分,它使得服务器能够灵活地返回动态生成的数据,从而实现页面的无刷新更新。这个方法与前端的JavaScript代码紧密配合,共同构建出...

    response.getWriter().write()向前台打印信息乱码问题解决

    response.getWriter().write() 功能:向前台页面显示一段信息。 当在普通的url方式中,会生成一个新的页面来显示内容。 当在ajax的方式中,会在alert中显示内容。 使用response.getWriter().write() 乱码问题 解决...

    response.setContentType

    PrintWriter out = response.getWriter(); out.println("&lt;html&gt;&lt;body&gt;Hello World!&lt;/body&gt;&lt;/html&gt;"); ``` - 设置一个 JPEG 图片: ```java response.setContentType("image/jpeg"); // ... 读取图片并发送到...

    Java Servlet输出中文乱码问题解决方案

    PrintWriter writer = response.getWriter(); writer.write(date); } ``` 这是因为字符流默认采用 ISO-8859-1 编码形式,该编码不支持中文。解决方法是使浏览器和服务器编码保持一致,例如: ```java private ...

    \java通过url在线预览Word、excel、ppt、pdf、txt文档中的内容

    然后使用 `WordExtractor` 将 Word 文档中的内容提取出来,最后使用 `response.getWriter().write()` 将内容输出到浏览器上。 Java 代码如下: ```java BufferedInputStream bis = null; URL url = null; ...

    servlet中文乱码问题

    通过修改Tomcat配置文件中的`URIEncoding`属性以及在Servlet中通过`response.setContentType()`和`request.setCharacterEncoding()`方法来设置字符编码,可以有效解决中文乱码问题。此外,还可以通过字符集转换的...

    实验1 JSP程序设计1.doc

    ### 实验一:JSP程序设计 #### 一、实验目的 ... #### 二、实验内容详解 ##### 1....- **实现思路**: ... - 使用JSP内置对象`out`来输出1到10的数字。... response.getWriter().println("&lt;html&gt;&lt;head&gt;&lt;title&gt;Multiplication...

    微信公众平台握手验证

    微信公众平台 握手验证 完整jsp if("GET".equals(request.getMethod())){//验证... response.getWriter().write(request.getParameter("echostr")); response.getWriter().flush(); response.getWriter().close(); }

    spring-security-core-2.0.5.RELEASE.jar修改使得满足ajax登录需求

    更改了RedirectUtils类的 ... response.getWriter().write(str.toString()); 因此无论是有跳转目标还是没有跳转目标 会往页面发送 { status:"true", url: "........." } 通过页面接收url来决定页面的走向。

    response设置的实例源码

    PrintWriter writer = response.getWriter(); writer.println("&lt;h1&gt;Hello, World!&lt;/h1&gt;"); writer.close(); ``` 七、示例代码 以下是一个简单的Servlet实例,展示了上述各种设置: ```java import javax.servlet.*;...

    微信公众平台的Java的开发详解(工程代码+解析).docx

    58.response.getWriter().write(result);59.return ;60.}61./** 如果不是接入验证,则处理微信发来的请求 */62.result = WechatProcess.processRequest(xml);63.response.getWriter().write(result);64.}65.66./**67...

    总结了java容易出错的信息 以及错误信息类型

    Java编程语言中,错误和异常处理是程序健壮性的重要组成部分。这些异常和错误可以分为不同的类别,帮助开发者识别和修复程序中的问题。以下是对Java中常见错误和异常类型的详细解释: 1. **算术异常类:Arithmetic...

    response

    `response.getWriter()`返回一个`java.io.PrintWriter`对象,可用于向客户端输出HTML、XML或其他文本内容。例如,`PrintWriter out = response.getWriter(); out.println("&lt;h1&gt;Hello, World!&lt;/h1&gt;");` 8. **设置...

    其他类别JSP无限级分类目录树-sorttree.zip

    JSP无限级分类目录树_sorttree /*覆盖doPost方法*/ public void doPost(HttpServletRequest request,... out=response.getWriter(); OpenConn(dsn,user,password); //建立连接 show(sql,0); closeConn(); }

    Jackson的jar包

    1、JackSon使用Java类中的getxxx()方法定位生成的json对象的属性xxx和值(返回值),而不是字段; 2、可以通过在Java类... response.getWriter.print(json); 或者 mapper.writeValue(Writer out,Object param);

    jsp返回登陆前的页面的几种实现

    本文将详细介绍几种实现这一功能的方法,并针对`response.sendRedirect()`在使用过程中遇到的一些常见问题给出解决方案。 #### 一、问题背景 在使用`response.sendRedirect()`进行页面跳转时,需要注意该方法调用...

    struts2返回JSON数据的两种方式

    总结,Struts2中返回JSON数据有两种主要方式:一是通过`response.getWriter().print()`手动输出JSON字符串;二是利用Struts2的内置JSON插件,通过返回特定的属性和结果类型自动处理JSON。每种方法都有其适用场景,...

    JSP内置对象request和response.doc

    在本文档中,我们将详细介绍request和response对象的功能和使用方法。 request对象 request对象是JSP内置对象中的一员,它用于处理客户端请求信息。在Servlet.service方法中,request对象作为参数传入,以便...

Global site tag (gtag.js) - Google Analytics