我的问题是,我想列出所有位置的图像 项目/src/main/webapp/images
我知道如果我知道图像名称,我可以制作这样的 URL:
assetSource.getContextAsset(IMAGESLOCATION + imageName, currentLocale).toClientURL();
但如果我不知道所有图片的名称怎么办?
提前感谢您的回答!
最佳答案
Web 应用程序(以及 Tapestry)不知道/不关心文件的绝对路径,因为它可以部署在任何地方。 你可以通过调用 HttpServletRequest 的 getRealPath 获取某个文件的绝对路径。
@Inject
private HttpServletRequest request;
...
// get root folder of webapp
String root = request.getRealPath("/");
// get abs path from any relative path
String abs = root + '/' + relPath;
HttpServletRequest 的 getRealPath 已弃用,建议使用 ServletContext.getRealPath 代替,但获取 ServletContext 并不容易。
我更喜欢使用 WebApplicationInitializer 实现
public class AbstractWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Here we store ServletContext in some global static variable
Global.servletContext = servletContext;
....
}
关于java - Tapestry 5.4 列出 webapp 文件夹中的所有图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42620652/