jsp中的基本路径
在我的应用程序中,我使用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”找到,
但是当其他人尝试访问我的应用程序时,他们使用:
现在,浏览器仍然尝试通过以下方式下载 test.js “http://localhost:8080/app/res/test.js”
当然是获取不到的。真正的路径应该是: http://192.168.xx:8080/app/res/test.js
既然“路径”是动作中的硬编码,有什么想法可以解决这个问题吗?
In my application,I use the struts2,and I create a base action to slove the path problem:
class BaseAction{
private String path;
static{
HttpServletRequest request = ServletActionContext.getRequest(); path=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();+"/";
}
}
Then all other action extend this baseaction.
In my jsp page,I add the path as the base:
xx.jsp:
....
<head>
<base href="<s:property value='path'/>">
<script ... src="res/test.js" />
</head>
It works well in my own machine.
http://localhost:8080/app
The test.js canbe found by "http://localhost:8080/app/res/test.js"
But when other people try to visit my app,they use:
now,the browser still try to download the test.js by
"http://localhost:8080/app/res/test.js"
Of course,it can not get it. The real path should be:
http://192.168.x.x:8080/app/res/test.js
SInce, the "path" is hard code in teh action,any idea to fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在静态初始化块中,我不希望您有可用的请求。我会这样做:
In a
static
initialization block I wouldn't expect you to have a request available. I'd do:怎么样
How about
您正在基本路径中设置本地主机和端口,而不是远程主机和端口。
毕竟这不是一个创建基本路径的好结构。我建议按如下方式创建它:
或者在 JSTL/EL 中很好地创建它
You're setting the local host and port in the base path instead of the remote host and port.
This is after all not a nice construct to create the base path. I'd suggest to create it as follows instead:
Or just nicely in JSTL/EL