window.open弹出窗口调用controller

时间:2023-12-28 13:42:38

前台图片调用js函数

<img src='${pageContext.request.contextPath}/FlatUI/img/link.png' id='report' alt='"+data[i].report+"' onclick='changeUrl(this,"+data[i].id+")' width=15px height=15px/>

js函数, 其中有一项重要处理, 在open窗口关闭的同时, 当前窗口刷新

function changeUrl(element,id){
//alert(element.alt);
//window.open("/trip/changeUrl?id="+id);
var winObj = window.open ("/portal/trip/changeUrl?id="+id+"&item="+element.id+"&value="+element.alt, "newwindow", "height=100, width=800, toolbar =no, menubar=no, scrollbars=no, resizable=no, location=no, status=no, top=50,left=1100");
var loop = setInterval(function() {
if(winObj.closed) {
clearInterval(loop);
//alert('closed');
window.location.reload();
}
}, 1);
}

controller端 /portal/trip/changeUrl

@RequestMapping("/changeUrl")
public String changeUrl(Long id, String item, String value, HttpServletRequest request){
request.setAttribute("id",id);
request.setAttribute("item",item);
request.setAttribute("value",value);
//System.out.println(id);
return "/tripController/tripURL";
}

tripURL.jsp, 其中有个处理, 提交保存后, 关闭当前页面

<form action="/portal/trip/updateTrip" onsubmit="window.opener=null;window.close();">
<font size="2" color="#004779"> Change URL To:</font><br/>
<input type="hidden" name="id" id="id" value="${id }">
<input type="hidden" name="item" id="item" value="${item }">
<input type="text" name="value" id="value" value="${value }" style="color:#95a5a6; padding-left:5px;border-radius:5px; width:700px; height:28px; vertical-align:middle;">
<input type="submit" value="Save" style="color:#2c3e50; font-size:12px;font-weight:bold; border-radius:5px; vertical-align:middle;height:30px; width:60px; "/>
</form>

controller端 /portal/trip/updateTrip

@RequestMapping("/updateTrip")
public String updateTrip(Long id,String item,String value,HttpServletResponse response){
//String result = "{\"result\":\"success\"}";
try {
URLDecoder.decode(value,"utf-8");
} catch (Exception e) {
e.printStackTrace();
} tripService.updateById(id,item,value); JSONObject data = new JSONObject();
try {
data.put("result", "success");
} catch (Exception e) {
System.out.println(e.getMessage());
} PrintWriter out = null;
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
try {
out=response.getWriter();
out.write(data.toString());
out.flush();
out.close();
return null;
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/trip/getAllTrip";
}