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
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:
String relativeWebPath = "/images";
String absoluteDiskPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath(relativeWebPath);
File imagesFolder = new File(absoluteDiskPath);
// ...
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 return null
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.
Additionally is it still fine to use h:datatTable to display data as I understand Tables to be somewhat outdated no?
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">
.