response.getWriter().write():有多个if都有(response.getWriter().write())的时候不写return还会继续向下执行,一般用有返回的函数,然后return null,如果是一个时不必函数有返回类型
ResponseUtil.writeToResponse实现Java回调js(这个类似ajax,其实就是ajax的Java回调的内部原理)
代替ajax的最原始的js方法
<script src="${pageContext.request.contextPath}/user/getUserName">
</script>
ResponseUtil.writeToResponse(response, userStr1);///////写到调用这个方法的页面
// 返回用户名写入主页。通过写入js回调js
@RequestMapping(value = "/user/getUserName", method = RequestMethod.GET)
public void getUserName(HttpSession session, HttpServletRequest request, Model model, HttpServletResponse response)
throws IOException, EsteelException {
String userName = CASUtil.getCurrentUserName(session);
if (null == userName) {
ResponseUtil.writeToResponse(response, "");
} else {
String userStr = "";
userStr += "我的Esteel [ " + userName + " ]";
userStr += "[<a class='pr pl5 pr5' onclick='showPersonalInfo();' href='/web/myaccount/getFirmCenter'>个人资料</a>]";
userStr += "<a class='pr pl5 none' href='javascript:QQLogin();' target='_blank'><img style='BORDER-BOTTOM: 0px; BORDER-LEFT: 0px; "
+ "BORDER-TOP: 0px; BORDER-RIGHT: 0px' class='vm ml5' src='"
+ request.getContextPath() + "/resources/images/icon/icon_qq_ico.gif'></a>";
String userStr1 = "$('div.login').fadeOut(100).html(\"" + userStr + "\").fadeIn(500);";
userStr1 += "$(function() {";
userStr1 += "var str=\"<script src='/esteelStatistics/user/addStatistics?u=" + userName
+ "&f=1&l=\"+encodeURIComponent(location.href)+\"'>\";";
userStr1 += "$(\"body\").append(str);";
userStr1 += "});";
ResponseUtil.writeToResponse(response, userStr1);///////写到调用这个方法的脚本标签内作为脚本,如果是ajax则写到dataType:html的默认承接页面,success回调函数中用,若果是src直接写到src中,
比如:<script 的src也就是引用js所以可以接收页面文件包含于标签内作为脚本代码(此时返回给他的一般
是js代码)同理<img src 即谁调用写回给谁,
}
}
java 回调js有两种方式(回调可用于回写数据到jsp域中,获取关闭打开的对话框)
1,直接把回掉的js代码返回 用:ResponseUtil.writeToResponse(response, userStr1)---》这个是回调js的本质
2,直接跳转到有些回调js代码的页面
login1.jso
<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
String rootPath = request.getContextPath();
request.setAttribute("rootPath",rootPath);
%>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<script type="text/javascript">
<!--
function close(){
try{
parent.loginSucess();//外层,上下中的上页面
}
catch(e){}
}
close();
//-->
</script>
</head>
</html>
<form action="${pageContext.request.contextPath}/account/tbCusFirmChg/uploadSave?allowFileType=${allowFileType}&callBackFunction=${callBackFunction}&virtualSavePath=${virtualSavePath}&fileType=${fileType}" method="post" enctype="multipart/form-data" >
<input type="file" name="file" id="file">
<input type="submit" value="上传" style="height:22px">
</form>
如果是form调用的话返回在此jsp页面(此时不<script src引用 由于已在<script>中不必写)这里还是需要拼<script>标签