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

标签 java javascript jquery jsp tomcat7

我正在使用页面上下文来引用 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/

相关文章:

java - Highcharts 单击时获取列上的颜色

javascript - 如何使用ng-class在angular js中应用css

javascript - 如果我加载另一个页面,我的 jQuery 将停止工作

jQuery ajax 请求 html 页面

java - JPA 无法反序列化 Java 8 LocalDateTime

c# - 从 Java 到 C# 的函数

javascript - 使用 JS 使用正则表达式验证用户输入时遇到问题

jQuery 1.3 只选择第一个元素

java - Spring Boot + Spring Security - 无法注销

javascript - 通过组合创建的对象上的方法无法访问所有属性