我正在使用页面上下文来引用 Tomcat Web 服务器上的某些资源。例如,我用它来获取图像,如下所示:
<img src="${pageContext.request.contextPath}/images/ajax-loader.gif" id="loading">
当我尝试对只是字符串的 src 执行相同的操作,而不是像上面那样位于文档标记内时,我收到错误。如何向纯字符串添加上下文?或者这是不允许的。
//This does not work and gives me syntax errors
if(response.validation === "success")
window.location.href=${pageContext.request.contextPath}+"dashboard.jsp";
else{
$("#notification").append("<td><center>You have entered an incorrect username or password</center></td>");
}
});
//This works but I don't want to do it this way
if(response.validation === "success")
window.location.href="http://69.164.xx.xx:8080/MySite/dashboard.jsp";
else{
$("#notification").append("<td><center>You have entered an incorrect username or password</center></td>");
}
});
//Edit: Rendered Page after using first option
if(response.validation === "success")
window.location.href=/RiverBoat+"dashboard.jsp";
else{
$("#notification").append("<td><center>You have entered an incorrect username or password</center></td>");
}
});
最佳答案
你的 JSP
window.location.href=${pageContext.request.contextPath}+"dashboard.jsp";
渲染到
window.location.href=/RiverBoat+"dashboard.jsp";
这是无效的 javascript,并且您会收到语法错误。
您希望将其渲染为
window.location.href="/RiverBoat"+"dashboard.jsp"; // or rather `"/RiverBoat" + "/dashb..."
所以只需添加引号
window.location.href="${pageContext.request.contextPath}"+"dashboard.jsp";
如上面代码注释中所述,您可能需要 "/dashboard.jsp"
。
关于java - 如何将页面上下文附加到 JSP 中不在 DOM 标记中的 URL 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25100777/