1

在我的应用程序中,我使用了 struts2,并创建了一个基本操作来解决路径问题:

class BaseAction{
  private String path;
  static{
  HttpServletRequest request = ServletActionContext.getRequest(); path=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();+"/";
  }
}

然后所有其他动作扩展这个基本动作。

在我的 jsp 页面中,我将路径添加为基础:

xx.jsp:

....
<head>
  <base href="<s:property value='path'/>">
  <script ... src="res/test.js" />
</head>

它在我自己的机器上运行良好。

http://localhost:8080/app test.js 可以通过“http://localhost:8080/app/res/test.js”找到

但是当其他人尝试访问我的应用程序时,他们使用:

http://192.168.xx:8080/app

现在,浏览器仍然尝试通过“http://localhost:8080/app/res/test.js”下载test.js

当然,它不能得到它。真实路径应该是: http://192.168.xx:8080/app/res/test.js

既然,“路径”是行动中的硬代码,有什么办法解决这个问题吗?

4

3 回答 3

3

static初始化块中,我不希望您有可用的请求。我会做:

<base href="${request.contextPath}" />
于 2011-03-30T11:16:32.180 回答
1

怎么样

<base href="${pageContext.request.contextPath}" />
于 2020-07-21T10:01:47.230 回答
0

您在基本路径中设置本地主机和端口,而不是远程主机和端口。

这毕竟不是创建基本路径的好结构。我建议按如下方式创建它:

path = request.getRequestURL().toString().replace(request.getRequestURI(), request.getContextPath()) + "/";

或者只是在 JSTL/EL 中很好

<c:set var="r" value="${pageContext.request}" />
<base href="${fn:replace(r.requestURL, r.requestURI, r.contextPath)}/" />
于 2011-03-30T12:31:56.313 回答