从 Maven Facelets 项目中的资源目录访问图像
首先,如果我以前问过这个问题,请原谅我(我因脑瘤而出现记忆问题。)
我正在尝试滚动托管 Bean 以从目录中获取图像,但不知道如何获取 iether 的 url以下位置:
1)Maven 'resources' dir & 2)webapp/images 目录(来自我的托管 bean。
此外,使用 h:datatTable
来显示数据仍然可以吗,因为我知道表格有点过时了不是吗?
我已经写了很大一部分所需的 bean,我只是在寻找有关如何(如果可能)查找 url 或将 url 设置为托管属性的信息,
;
/**
* Created by IntelliJ IDEA.
* User: Yucca http://thejarbar.org
* Date: 2/23/12
* Time: 10:21 AM
* To change this template use File | Settings | File Templates.
*/
import org.apache.commons.collections.ArrayStack;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import java.io.File;
import java.util.Arrays;
import java.util.List;
/**
* Contain utility methods to interact with images.
*/
@ManagedBean(name = "images")
public class ImageBean {
List<File> images;
static String thumbUrl;
static String fullSizeURL;
/**
* Returns all thumbnails at specified url
*/
public static List<File> thumbnails (){
thumbUrl=//can I get it from context if I can't set url as a manages property?
File[] files = new File(thumbUrl).listFiles();
List<File> returnList = Arrays.asList(files);
return returnList;
}
/**
* Returns all full-sized images at specified url
*/
public static List<File> fullSized (){
thumbUrl=**//can I get it from context if I can't set url as a manages property?**
File[] files = new File(thumbUrl).listFiles();
List<File> returnList = Arrays.asList(files);
return returnList;
}
}
非常感谢,Yucca
Firstly, please forgive me if I have asked this before (i suffer memory problems due to a brain tumor.)
I am trying to roll a Managed Bean to fetch images from a directory, but don't know how to get the url for iether of the following locations:
1)Maven 'resources' dir &
2)webapp/images dir (from withing my Managed bean.
Additionally is it still fine to use h:datatTable
to display data as I understand Tables to be somewhat outdated no?
I have written a large portion of the desired bean and am just seeking info on how (if possible) to find the urls or set the url as a managed property.
package com.buhaugane.util;
/**
* Created by IntelliJ IDEA.
* User: Yucca http://thejarbar.org
* Date: 2/23/12
* Time: 10:21 AM
* To change this template use File | Settings | File Templates.
*/
import org.apache.commons.collections.ArrayStack;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import java.io.File;
import java.util.Arrays;
import java.util.List;
/**
* Contain utility methods to interact with images.
*/
@ManagedBean(name = "images")
public class ImageBean {
List<File> images;
static String thumbUrl;
static String fullSizeURL;
/**
* Returns all thumbnails at specified url
*/
public static List<File> thumbnails (){
thumbUrl=//can I get it from context if I can't set url as a manages property?
File[] files = new File(thumbUrl).listFiles();
List<File> returnList = Arrays.asList(files);
return returnList;
}
/**
* Returns all full-sized images at specified url
*/
public static List<File> fullSized (){
thumbUrl=**//can I get it from context if I can't set url as a manages property?**
File[] files = new File(thumbUrl).listFiles();
List<File> returnList = Arrays.asList(files);
return returnList;
}
}
Many thanks, Yucca
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
ExternalContext#getRealPath()
将给定的相对 Web 路径转换为绝对磁盘文件系统。因此,要获取公共 webcontent 中/images
文件夹的绝对磁盘文件系统路径,请执行以下操作:您应该记住,当服务器配置为扩展 WAR 时,上述操作将不起作用进入内存而不是磁盘。然后,
getRealPath()
在所有情况下都会返回null
。您需要寻找替代方法,例如将其存储在 WAR 之外的固定路径上,或者很可能存储在数据库中。表格并没有过时。它们仍然非常适合呈现表格数据。仅使用表格进行布局确实被认为是不好的做法,因为它不符合语义,因此不利于 SEO。在 JSF 术语中,我们讨论使用
来提供页面布局(例如页眉、正文、页脚等)。您应该使用来代替,它可以由
生成。You could use
ExternalContext#getRealPath()
to convert a given relative web path to an absolute disk file system. So to get the absolute disk file system path of the/images
folder in public webcontent, do as follows:You should only keep in mind that the above won't work when the server is configured to expand WAR into memory instead of on disk. The
getRealPath()
would then returnnull
in all circumstances. You'd need to look for an alternative approach, for example storing it outside the WAR on a fixed path, or very maybe in a database.Tables are not outdated. They are still perfect for presenting tabular data. Only using tables for layout is indeed considered bad practice as it's not semantic and thus poor for SEO. In JSF terms, we're then talking about using
<h:panelGrid>
to give the page layout (e.g. header, body, footer, etc). You should be using<div>
s instead which can be generated by<h:panelGroup layout="block">
.