从 Maven Facelets 项目中的资源目录访问图像

发布于 2025-01-08 09:42:38 字数 1656 浏览 1 评论 0原文

首先,如果我以前问过这个问题,请原谅我(我因脑瘤而出现记忆问题。)

我正在尝试滚动托管 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

草莓酥 2025-01-15 09:42:38

可以使用ExternalContext#getRealPath() 将给定的相对 Web 路径转换为绝对磁盘文件系统。因此,要获取公共 webcontent 中 /images 文件夹的绝对磁盘文件系统路径,请执行以下操作:

String relativeWebPath = "/images";
String absoluteDiskPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath(relativeWebPath);
File imagesFolder = new File(absoluteDiskPath);
// ...

您应该记住,当服务器配置为扩展 WAR 时,上述操作将不起作用进入内存而不是磁盘。然后,getRealPath() 在所有情况下都会返回 null。您需要寻找替代方法,例如将其存储在 WAR 之外的固定路径上,或者很可能存储在数据库中。


此外,使用 h:datatTable 来显示数据是否仍然可以,因为我知道表格有些过时了,不是吗?

表格并没有过时。它们仍然非常适合呈现表格数据。仅使用表格进行布局确实被认为是不好的做法,因为它不符合语义,因此不利于 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:

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">.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文