使用MyFaces Tomahawk + ​​JSF 2.0上传文件 [英] File uploading using MyFaces Tomahawk + JSF 2.0

查看:195
本文介绍了使用MyFaces Tomahawk + ​​JSF 2.0上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在用Tomahawk MyFaces上传文件到我的服务器。我按照一步一步的指示,一切似乎好,但我没有得到的文件坚持到数据base.I没有看到任何错误,我只是看到我的eclipse控制台中的一些警告。有人可以看看吗?这是我做的:

1 - 为JSF 2.0下载了Tomahawk,并将所有的.jar文件添加到了我的WEB-INF / lib文件夹中。



<2>我检查了我的web.xml配置正确,使用了Faces Servlet。而且我还添加了一个formahawks扩展的过滤器这是它的样子:

 < 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>
<加载启动> 1< /加载启动>
< / 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>我也检查了我的faces-config.xml是正确的:

 <?xml version =1.0encoding =UTF-8?> 
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 - 用于上传小工具的简单复合页面

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // EN
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
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 =submitaction =#{uploadController.submit}/>
< h:messages />
< / h:表格>
< / ui:define>
< / ui:composition>
< / html>

5 - 我有一个托管的bean与页面进行通信并获取输入的文件:

  @ManagedBean 
@RequestScoped
公共类UploadController {

@EJB
private IFileUploaderEJB fileUploaderEJB;

私人UploadedFile uploadedFile;
$ b $ public void submit()throws IOException {
String fileName = FilenameUtils.getName(uploadedFile.getName());
String contentType = uploadedFile.getContentType();
byte [] bytes = uploadedFile.getBytes();

//现在你可以在DB中保存字节(也是内容类型?)
垃圾垃圾= new Garbage();
garbage.setFilename(fileName);
garbage.setFile(bytes);
garbage.setDescription(关于文件的信息);
garbage.setFileType(文件扩展名);
fileUploaderEJB.uploadGarbage(垃圾);

FacesContext.getCurrentInstance()。addMessage(
null,
)新建FacesMessage(String.format(
)'%s'类型的文件'%s'成功上传!,
fileName,contentType)));
}

public UploadedFile getUploadedFile(){
return uploadedFile;
}

public void setUploadedFile(UploadedFile uploadedFile){
this.uploadedFile = uploadedFile;
}}

注意:托管bean调用一个应该保存在数据库中的EJB文件

6 - 允许访问数据库的EJB:

  @Stateless(name =ejbs / FileUploaderEJB)
public class FileUploaderEJB implements IFileUploaderEJB {
@PersistenceContext
private EntityManager em;


public Garbage uploadGarbage(垃圾垃圾){
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat(dd / MM / yyyy HH:mm:ss);

garbage.setUploadDate(dateFormat.format(date));
// ...

em.persist(垃圾);

返回垃圾;





最后我有一个使用JPA注释的实体,它使用@Lob将文件存储在数据库中:

  @Entity 
public class Garbage {
@Id
@GeneratedValue
@Column(nullable = false)
私有长ID;
@Column(nullable = false)
私有字符串文件名;
@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)
私有字符串描述;
// Getters and Setters ...

问题是3:



P1 - 当我用浏览按钮选择一个文件,然后点击提交没有任何反应。我没有看到控制台中的任何输入查询,没有新行添加到数据库。我缺少什么?



P2 - 每当我在源代码中进行更改并再次发布控制台时, (近40秒),它显示了很多像这样的警告(20或多或少):
$ b


警告:JSF1029:应用程序是版本为2.0(或者显式地通过/WEB-INF/faces-config.xml的版本或者缺少/WEB-INF/faces-confg.xml),然而是类org.ajax4jsf.taglib.html.facelets。 AjaxSupportHandler取决于传统的facelet类。

jar文件或者面部配置有问题吗? -config.xml?



P3 - 当我浏览我的项目中的任何页面时,
$ b


警告:PWC4011:无法将上下文/数据池中的请求字符编码设置为UTF-8,因为请求参数已被读取ServletRequest.getReader()已经被调用了

我认为这与web.xml中的过滤器有关。

解决方案


当我用浏览按钮选择一个文件,然后点击提交什么都不会发生。您的< h:form> 缺少<



< c $ c> enctype =multipart / form-data
属性。检查迷你教程在这里: JSF 2.0文件上传与战斧的 < t:inputFileUpload>
$ b


警告:JSF1029:在2.0(明确的版本/WEB-INF/faces-config.xml或缺乏/WEB-INF/faces-confg.xml),但类'org.ajax4jsf.taglib.html.facelets.AjaxSupportHandler '取决于传统的facelet类。


您还有一些以JSF 1.2为目标的RichFaces / A4J在您的Web项目中的库。如果您不需要,请去掉它们。
$ b


警告:PWC4011:无法将请求字符编码设置为UTF- 8来自上下文/数据池,因为请求参数已经被读取,或者ServletRequest.getReader()已经被调用了


这是Glassfish特有的。 Glassfish默认为ISO-8859-1,需要重新配置才能使用UTF-8。另请参阅如何摆脱警告:PWC4011:无法将请求字符编码设置为UTF-8


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:

1-Downloaded Tomahawk for JSF 2.0 and added all the .jars to my WEB-INF/lib folder

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:

<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:

<?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

<!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:

@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

6-An EJB to allow access to database:

    @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:

    @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:

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?

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):

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.

Is there something wrong with the jar files or with the configuration at the faces-config.xml?

P3 - When i navigate to any of the pages in my project ussing the browser, a warning like this one:

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

I think it has something to do with the filter at web.xml

解决方案

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

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.

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.

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

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

这篇关于使用MyFaces Tomahawk + ​​JSF 2.0上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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