简体   繁体   English

返回JSF页面上的图像列表

[英]Returning list of images on JSF page

I have a dropdown list of items a user can select from (the view is JSF). 我有一个用户可以选择的项目下拉列表(视图是JSF)。 I would like for an image to appear on the same JSF page after a user selects an item from the dropdown list (ie A user select the word "Cat" from the dropdown list, and group of different cat images appear) 我希望在用户从下拉列表中选择一个项目后,图像出现在同一个JSF页面上(即用户从下拉列表中选择单词“Cat”,并显示一组不同的猫图像)

How would I code this in JSF? 我将如何在JSF中编写代码?

Note* I'm using JSF 2.0 with facelets, not JSPs. 注意*我正在使用JSF 2.0与facelets,而不是JSP。

Provide a list with image URL's in the dropdown and use h:graphicImage to display an image on the selected URL. 在下拉列表中提供包含图像URL的列表,并使用h:graphicImage在所选URL上显示图像。 Then, use f:ajax to re-render the image on change the dropdown. 然后,使用f:ajax重新渲染图像,更改下拉列表。

Here's a kickoff example: 这是一个启动示例:

<h:form>
    <h:selectOneMenu value="#{bean.imageURL}">
        <f:selectItems value="#{bean.imageURLs}" />
        <f:ajax event="change" render="image" />
    </h:selectOneMenu>
    <h:graphicImage id="image" value="#{bean.imageURL}" /> 
</h:form>

Bean: 豆:

private List<String> imageURLs; // +getter
private String imageURL; // +getter +setter

BalusC's answer is (as always :) correct, but as you noticed there will be a list of URLs in the selectOneMenu. BalusC的答案是(一如既往:)正确,但正如您所注意到的那样,selectOneMenu中会有一个URL列表。 That is exactly why I asked you how do you store your images. 这就是为什么我问你如何存储你的图像。 The way I usually do it: (and from what I know that's the standard way of doing it, hopefully someone will correcty me if I'm wrong) you store the image somewhere on the server and in your DB you store its location. 我通常这样做的方式:(根据我所知道的标准方式,希望有人会纠正我,如果我错了)你将图像存储在服务器上的某个位置,并存储在存储其位置的数据库中。 That's why I would suggest to make a MyImage class (which will be mapped to a DB table) where you would store the name of the image and a way to get its location on the server (for example you can do it using namespaces like cats will have a String namespace = "cats" and a String imageName and a method that will return the url like String getImageLocation() {return " http://something.com/images/ "+namespace+"/"+imageName;} remember that it's important to make it look like a getter so the JSF can use it). 这就是为什么我建议创建一个MyImage类(将映射到数据库表),您将存储图像的名称以及在服务器上获取其位置的方法(例如,您可以使用像猫这样的名称空间来实现它将有一个String namespace = "cats"和一个String imageName以及一个返回url的方法,如String getImageLocation() {return " http://something.com/images/ "+namespace+"/"+imageName;}记住让它看起来像一个吸气剂是很重要的,所以JSF可以使用它)。 Then all you have to do is to get the list of MyImages for a given namespace from your DB and render the images in a dataTable, something like this: 然后,您所要做的就是从数据库中获取给定命名空间的MyImages列表,并在dataTable中呈现图像,如下所示:

<h:dataTable value="#{myBeanWithAListOfImages.images}" var="img">
    <h:column>
        <h:graphicImage value="img.imageLocation"/>
    </h:column>
</h:dataTable>

Where images is a List<MyImage> . 其中图像是List<MyImage> This should work and print the images in a single column. 这应该工作并在单列中打印图像。

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

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