JSF从嵌套子文件夹加载上载的图像 [英] JSF Load uploaded images from nested subfolders

查看:130
本文介绍了JSF从嵌套子文件夹加载上载的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网络应用程序中,我想上传图像,因此使用p:graphicImage显示它们。我将上传的图像存储在Web应用程序之外 - 特别是在E:\ webapp \ upload。请注意,我的tomcat服务器安装在E:卷上。我创建了一个ImageServlet来读取图像,如 http://balusc.blogspot.com /2007/04/imageservlet.html

In my web application, I want to upload images and hence present them using p:graphicImage. I store the uploaded images outside the web app – in particular at E:\webapp\upload. Note that my tomcat server is installed on E: volume. I have created an ImageServlet to read the images as described in http://balusc.blogspot.com/2007/04/imageservlet.html.

我在web.xml文件中添加了以下配置:

I added the following configuration to my web.xml file:

<servlet>
    <servlet-name>imageServlet</servlet-name>
    <servlet-class>utilities.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>imageServlet</servlet-name>
    <url-pattern>/upload/*</url-pattern>
</servlet-mapping>

我可以使用upload / imageName.ext访问index.xhtml页面中的图像,例如:

I can access the images from the index.xhtml page using "upload/imageName.ext" eg:

<p:graphicImage value="upload/flag.gif" />

此jsf代码已正确转换为以下HTML代码:

This jsf code is correctly converted into the following HTML code:

<img src="http://localhost:8080/MyApp/upload/flag.gif?pfdrid_c=true">

但是,我希望能够从包含在子文件夹中的页面访问图像。例如,我有一个位于admin子文件夹的页面:

However, I want to be able to access images from pages which are contained into subfolders. Eg I have one page which is located at "admin" subfolder:

http://localhost:8080/MyApp/admin/language.xhtml  

在该页面内,相同的jsf代码转换为:

Inside that page, the same jsf code is converted into:

<img src="http://localhost:8080/MyApp/admin/upload/flag.gif?pfdrid_c=true"> 

这是错误的。

在其他单词,它在E:\ webapp \ adminmin \ upload目录中查找不存在的图像,因此不显示任何内容。我可以清楚地写下面的代码:

In other words, it looks for the images at "E:\webapp\admin\upload" directory which does not exist and hence nothing is displayed. I can clearly write the following code:

<p:graphicImage value="../upload/flag.gif" /> 

让它运作但我认为这是一种不好的做法,因为我必须采用每个路径我在不同的子文件夹层次结构中(例如考虑嵌套子文件夹的问题)。

to make it work but I consider this as a bad practice because I will have to adopt the path each time I am in a different subfolder hierarchy (consider for example the problem of nested subfolders).

我想知道是否有任何方法可以指定图像的相对路径,例如upload / flag.gif,无论子文件夹层次结构如何。

I am wondering if there is any way to just specify the relative path of the image eg "upload/flag.gif" regardless of subfolder hierarchy.

提前致谢!

推荐答案

让路径以 / 开头,使其相对于

Just let the path start with / to make it relative to the context root instead of the currently opened folder.

<p:graphicImage value="/upload/flag.gif" />

这篇关于JSF从嵌套子文件夹加载上载的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆