简体   繁体   English

从struts2操作变量中的路径以jsp显示图像

[英]display image in jsp from path in struts2 action variable

i have a struts2 action which has a variable for an image which i want to display in a jsp page. 我有一个struts2动作,该动作具有要在jsp页面中显示的图像的变量。 when i display the value of the variable using the following: 当我使用以下命令显示变量的值时:

<s:property value="variableName" />

i get the absolute path of the image file. 我得到图像文件的绝对路径。

but when i try the following: 但是当我尝试以下操作时:

<img src='<s:property value="variableName" />' />

i get a blank there. 我在那里一片空白。 i have even tried the following: 我什至尝试了以下方法:

<img src="${variableName}" />

EDIT: i have done some thinking. 编辑:我做了一些思考。 the path of the image which is set is in temp folder of tomcat. 所设置图像的路径在tomcat的temp文件夹中。 is it because of that that i am unable to access it? 是因为我无法访问它吗?

to no effect. 没有任何作用。 what may be the issue in this? 这可能是什么问题?

The src of the img tag must contain an absolute path, then only it can point to the image file from the client machine. img标签的src必须包含绝对路径,然后只有它才能指向客户端计算机上的图像文件。

If the variableName contains an absolute path like this 如果variableName包含这样的绝对路径

http://localhost/images/pics/icon.gif

The img will finds the image from the server. img将从服务器找到图像。 Thus available always an hence you can use this. 因此始终可用,因此您可以使用它。

<img src="${variableName}"/>

If the variableName is a relative path ie; 如果variableName是相对路径,即; something that's recognisable for the server. 服务器可以识别的内容。

/images/pics/icon.gif

The above path will be identified by the server but it wont be identified from the remote machine. 上面的路径将由服务器标识,但不会从远程计算机上标识。 In that case you need to have this 在那种情况下,你需要这个

<img src="<c:url value=variableName/>" />

Always keep in mind when doing web development what you have in server context and what travels to the client browser. 在进行Web开发时,请始终牢记服务器上下文中包含的内容以及传递给客户端浏览器的内容。 If you do what you are suggesting, you will render and send to the client something like this: 如果您按照建议进行操作,则将呈现以下内容并将其发送给客户端:

<img src="c:\my_local_directory\some_image.jpg"></img>

Which means nothing to the client browser, which is surely executing in some other machine. 这对于客户端浏览器毫无意义,它肯定会在其他计算机上执行。

You should store your images somewhere where your application server can show them, and give always paths that are relative to your web application. 您应该将图像存储在应用服务器可以显示图像的位置,并始终提供相对于Web应用程序的路径。 For example, if you have stored some_image.jpg straight in your my-app.war file, something like: 例如,如果您直接在my-app.war文件中存储some_image.jpg,则类似于:

<img src="/my-app/some_image.jpg"></img>

will always work. 将始终有效。 If you need to refine that image (imagine you need to put a watermark on it before) or need to recover its contents from somewhere (a BLOB in a database or something like that), you will need an entire new action in order to do it, for example: 如果您需要优化该图像(想象之前需要在其上加水印)或需要从某个位置(数据库中的BLOB或类似的东西)恢复其内容,则需要执行一个全新的操作它,例如:

<img src="/my-app/ImageAction.action?image=some_image"></img>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM