如何在JSP中将页面上下文附加到不在DOM标记中的URL字符串

How do you append page context to URL String in JSP that is not in a DOM tag

本文关键字:DOM URL 字符串 JSP 上下文      更新时间:2023-09-26

我使用页面上下文来引用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" .