JSF中文件浏览的替代方法是什么? [英] What is the alternative of file browse in JSF?

查看:47
本文介绍了JSF中文件浏览的替代方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们一直在使用Netbeans 6.9.1在JSF中开发一个Web应用程序.这是关于购物.因此,很多时候我们必须处理图像.图像需要上传到MySql数据库中.这样做需要某种机制来浏览图像文件,但是我们发现JSF不直接支持文件浏览.我们本可以使用HTML文件浏览< input type ="file" .../> 的,但是为此,我们需要从Servlet获取外部上下文,以从请求访问JSF托管bean中的值参数.

We have been developing a web application in JSF using Netbeans 6.9.1. It is about shopping. Therefore, many a times we have to deal with images. Images need to be uploaded into MySql database. Doing so, requires some mechanism to browse image files but we found that JSF does not support file browse directly. we could have used HTML file browse <input type="file".../> but for that we need to obtain the external context from Servlet to access it's value in JSF managed bean from request parameters.

HttpServletRequest request=(HttpServletRequest)FacesContext
                         .getCurrentInstance().getExternalContext().getRequest();

这可能不是我认为的便捷,建议和最佳方法之一,[而且可能不是将HTML与JSF组件混合的方法],如果我们要使用HTML文件浏览,则必须维护在JSF中最重要的是视图状态,而HTML文件浏览本质上并不能保持其视图状态,而且,我们只能从HTML文件浏览中获取文件名.在许多情况下,获取绝对文件路径至关重要.是否可以从HTML文件浏览器检索绝对文件路径并使其保持其视图状态?我们应该采用哪种方法?

which may not be one of the convenient, suggested and best methods I think [ and also, it may not be the approach to mix HTML with JSF components] and if we were to use HTML file browse, we would have to maintain it's view state which is utmost important in JSF and HTML file browse by nature doesn't maintain it's view state and also, we can only obtain the file name from HTML file browse. In many circumstances, it is essential to obtain the absolute file path. Is it possible to retrieve the absolute file path from HTML file browse and make it maintain it's view state? Which approach should we follow?

推荐答案

在JSF代码中摆弄原始Servlet API的确不是最佳实践.您应该尽量减少 javax.servlet 的导入/依赖关系.理想的托管bean不包含那些导入.所以忘记那一部分.

It's indeed not the best practice to fiddle with raw Servlet API in your JSF code. You should try to minimize the javax.servlet imports/dependencies as much as possible. An ideal managed bean has none of those imports. So forget that part.

实际上标准JSF组件库不附带表示<输入类型文件"> 的组件,原因很简单,基于JSF的标准Servlet API未提供任何内容解析 multipart/form-data 请求的工具.仅从Servlet 3.0版开始,新的 HttpServletRequest#getParts() 方法.但是到那时,JSF 2.0已经设计完成.我们目前使用的是JSF 2.1,最终正式要求最低的Servlet 3.0,因此仅使用标准API即可实现文件上传组件.现在,有两个正在进行的规范请求正在打开,以在JSF 2.2中包括一个文件上传组件:

The standard JSF component library indeed doesn't ship with a component which represents a <input type"file">, for the simple reason that the standard Servlet API, which JSF is based on top of, didn't provide any facilities to parse multipart/form-data requests. Only since Servlet version 3.0 this is supported with the new HttpServletRequest#getParts() method. But at that point JSF 2.0 was already designed and finished. We're currently at JSF 2.1, which finally officially requires a minimum of Servlet 3.0, so a file upload component should be possible with just the standard APIs. Right now there are two ongoing spec requests open to include a file upload component in JSF 2.2:

直到那时,您最好的选择是获取第3方组件库.如果您正在寻找一个不会导致任何其他JS/CSS幻想的真正准系统组件,那么我建议您选择 < t:inputFileUpload> 组件.您可以在以下文章中找到如何安装/配置它(尚不清楚您使用的是哪个JSF版本,因此我在JSF 1.x和2.x中都提到了它):

Until then, your best bet is to grab a 3rd party component library. If you are looking for a really barebones component which doesn't render any additional JS/CSS fanciness, then I'd recommend to pick Tomahawk's <t:inputFileUpload> component. You can find in the following articles how to install/configure it (it's unclear what JSF version you're using, so I'm mentioning it for both JSF 1.x and 2.x):

请注意,关于如何获取绝对路径的问题使我的颈部毛发有些起伏.您应该对文件的绝对路径不感兴趣,而对文件的内容感兴趣.有关更详细的说明,另请参见

Please note that your question as in how to retrieve the absolute path made my neck hairs to raise somewhat. You should not be interested in the file's absolute path, but in the file's contents. For a more detailed explanation, see also How to get the file path from HTML input form in Firefox 3.

这篇关于JSF中文件浏览的替代方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆