java 使用 MyFaces Tomahawk + JSF 2.0 上传文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5431512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File uploading using MyFaces Tomahawk + JSF 2.0
提问by sfrj
I am currently ussing Tomahawk MyFaces to upload files in to my server. I followed some instructions step by step and all seems ok, but i dont get the file persisted into the data base.I dont see any error, i just see some warnings in my eclipse console. Could someone have a look? This is what i did:
我目前正在使用 Tomahawk MyFaces 将文件上传到我的服务器。我一步一步地遵循了一些说明,一切似乎都没有问题,但是我没有将文件保存到数据库中。我没有看到任何错误,我只是在我的 Eclipse 控制台中看到了一些警告。有人可以看看吗?这就是我所做的:
1-Downloaded Tomahawk for JSF 2.0 and added all the .jars to my WEB-INF/lib folder
1-为 JSF 2.0 下载 Tomahawk 并将所有 .jars 添加到我的 WEB-INF/lib 文件夹
2-I checked that my web.xml is correctly configured to use the Faces Servlet. And also i added a filter for tomahawks extensions This is how it looks like:
2-我检查了我的 web.xml 是否正确配置为使用 Faces Servlet。我还为 tomahawks 扩展添加了一个过滤器这是它的样子:
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/pages/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>pages/index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>MyFacesExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFacesExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
</web-app>
3-I also checked that my faces-config.xml is correct:
3-我还检查了我的 faces-config.xml 是否正确:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
4-I created a very simple composite page for the upload gadget
4-我为上传小工具创建了一个非常简单的复合页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:t="http://myfaces.apache.org/tomahawk">
<ui:composition template="WEB-INF/templates/BasicTemplate.xhtml">
<ui:define name="uploadForm">
<h:form>
<t:inputFileUpload value="#{uploadController.uploadedFile}" />
<h:commandButton value="submit" action="#{uploadController.submit}" />
<h:messages />
</h:form>
</ui:define>
</ui:composition>
</html>
5-I have a managed bean to comunicate with the page and get the inputed file:
5-我有一个托管bean与页面通信并获取输入的文件:
@ManagedBean
@RequestScoped
public class UploadController {
@EJB
private IFileUploaderEJB fileUploaderEJB;
private UploadedFile uploadedFile;
public void submit() throws IOException {
String fileName = FilenameUtils.getName(uploadedFile.getName());
String contentType = uploadedFile.getContentType();
byte[] bytes = uploadedFile.getBytes();
// Now you can save bytes in DB (and also content type?)
Garbage garbage = new Garbage();
garbage.setFilename(fileName);
garbage.setFile(bytes);
garbage.setDescription("info about the file");
garbage.setFileType("File extension");
fileUploaderEJB.uploadGarbage(garbage);
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(String.format(
"File '%s' of type '%s' successfully uploaded!",
fileName, contentType)));
}
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}}
Note: The managed bean calls an EJB that should persist into the database the file
注意:托管 bean 调用一个 EJB,该 EJB 应该将文件持久化到数据库中
6-An EJB to allow access to database:
6-允许访问数据库的EJB:
@Stateless(name = "ejbs/FileUploaderEJB")
public class FileUploaderEJB implements IFileUploaderEJB {
@PersistenceContext
private EntityManager em;
public Garbage uploadGarbage(Garbage garbage) {
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
garbage.setUploadDate(dateFormat.format(date));
//...
em.persist(garbage);
return garbage;
}
7- And finally i have an Entity that uses JPA annotations and it uses a @Lob to store the file in the database:
7- 最后我有一个使用 JPA 注释的实体,它使用 @Lob 将文件存储在数据库中:
@Entity
public class Garbage {
@Id
@GeneratedValue
@Column(nullable = false)
private Long id;
@Column(nullable = false)
private String filename;
@Column(nullable = false)
private String fileType;
@Column(nullable = false)
private String uploadDate;
@Column(nullable = false)
private String destroyDate;
@Lob
@Column(nullable = false)
private byte[] file;
@Column(nullable = false)
private String description;
//Getters and Setters...
The problems are 3:
问题是3:
P1- When i select a file with the browse button and then i click submit nothing happens. I dont see any input query in the console,no new rows are added to the database. What i am missing?
P1- 当我用浏览按钮选择一个文件,然后我点击提交什么也没有发生。我在控制台中没有看到任何输入查询,也没有向数据库添加新行。我缺少什么?
P2- Whenever i make a change in my source code and publish again the console it takes longer than usual to build(almost 40 secs) and it displays me lots of warnings like this one(20 more or less):
P2- 每当我对源代码进行更改并再次发布控制台时,它的构建时间比平时要长(将近 40 秒),并且它会向我显示很多这样的警告(或多或少 20 个):
WARNING: JSF1029: Application is versioned at 2.0 (either explicitly by the version of /WEB-INF/faces-config.xml or the lack of a /WEB-INF/faces-confg.xml), however class 'org.ajax4jsf.taglib.html.facelets.AjaxSupportHandler' depends on a legacy facelet class. The facelet artifact represented by this class will not be registered.
警告:JSF1029:应用程序的版本为 2.0(明确通过 /WEB-INF/faces-config.xml 的版本或缺少 /WEB-INF/faces-confg.xml),但是类 'org.ajax4jsf. taglib.html.facelets.AjaxSupportHandler' 依赖于遗留的 facelet 类。此类表示的 facelet 工件将不会被注册。
Is there something wrong with the jar files or with the configuration at the faces-config.xml?
jar 文件或faces-config.xml 中的配置有问题吗?
P3- When i navigate to any of the pages in my project ussing the browser, a warning like this one:
P3- 当我使用浏览器导航到项目中的任何页面时,会出现如下警告:
WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /Datapool, because request parameters have already been read, or ServletRequest.getReader() has already been called
警告:PWC4011:无法从上下文/数据池将请求字符编码设置为 UTF-8,因为请求参数已经被读取,或者 ServletRequest.getReader() 已经被调用
I think it has something to do with the filter at web.xml
我认为这与 web.xml 中的过滤器有关
回答by BalusC
When i select a file with the browse button and then i click submit nothing happens.
当我使用浏览按钮选择一个文件,然后单击提交时什么也没有发生。
Your <h:form>
is missing the enctype="multipart/form-data"
attribute. Check the mini tutorial here: JSF 2.0 file upload with Tomahawk's <t:inputFileUpload>
.
您<h:form>
缺少该enctype="multipart/form-data"
属性。在这里查看迷你教程:JSF 2.0 file upload with Tomahawk's<t:inputFileUpload>
.
WARNING: JSF1029: Application is versioned at 2.0 (either explicitly by the version of /WEB-INF/faces-config.xml or the lack of a /WEB-INF/faces-confg.xml), however class 'org.ajax4jsf.taglib.html.facelets.AjaxSupportHandler' depends on a legacy facelet class. The facelet artifact represented by this class will not be registered.
警告:JSF1029:应用程序的版本为 2.0(明确通过 /WEB-INF/faces-config.xml 的版本或缺少 /WEB-INF/faces-confg.xml),但是类 'org.ajax4jsf. taglib.html.facelets.AjaxSupportHandler' 依赖于遗留的 facelet 类。此类表示的 facelet 工件将不会被注册。
You still have some JSF 1.2-targeted RichFaces/A4J libs in your web project. Get rid of them if you don't need it.
您的 Web 项目中仍然有一些针对 JSF 1.2 的 RichFaces/A4J 库。如果您不需要,请摆脱它们。
WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /Datapool, because request parameters have already been read, or ServletRequest.getReader() has already been called
警告:PWC4011:无法从上下文/数据池将请求字符编码设置为 UTF-8,因为请求参数已经被读取,或者 ServletRequest.getReader() 已经被调用
This is Glassfish specific. Glassfish defaults to ISO-8859-1 and needs to be reconfigured to use UTF-8. See also How to get rid of WARNING: PWC4011: Unable to set request character encoding to UTF-8
这是 Glassfish 特有的。Glassfish 默认为 ISO-8859-1,需要重新配置为使用 UTF-8。另请参阅如何摆脱警告:PWC4011:无法将请求字符编码设置为 UTF-8