Tommahawk Myfaces和CDI [英] Tommahawk Myfaces and CDI

查看:189
本文介绍了Tommahawk Myfaces和CDI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试创建JSF页面时,我在使用带有 Tommahawk Myfaces 的CDI时遇到了一些问题上传一个文件。提到这个问题,看起来Tommahawk MyFaces与CDI不兼容,但是这是正确的吗?

I'm having some problems using CDI with Tommahawk Myfaces when trying to create a JSF page to upload a file. Referring to this question it looks as though Tommahawk MyFaces isn't compatible with CDI but is this correct?

My Bean是这样的:

My Bean is like this:

@ManagedBean
@RequestScoped
public class Bean {
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?)

    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;
    }
}

但是一旦我更换 @ManagedBean @RequestScoped @Named @模型部署时我收到以下警告:

But as soon as I replace @ManagedBean and @RequestScoped with @Named or @Model I get the following warning when deploying:


WELD-001529为类组织创建了一个InjectionTarget实现.apache.myfaces.webapp.filter.TomahawkFacesContextFactory没有任何合适的构造函数。

WELD-001529 An InjectionTarget implementation is created for a class org.apache.myfaces.webapp.filter.TomahawkFacesContextFactory which does not have any appropriate constructor.

当我上传一个包含以下内容的文件时当我使用 @Named @Model <时,JSF页面, UploadedFile 的值为null / code>。但不能使用 @ManagedBean'和 @RequestScope 。这是 .xhtml`文件,我确实使用了tommahawk Myfaces:

And when I upload a file with the following JSF page, the value of UploadedFile is null when I use @Named or @Model. but not with @ManagedBean' and@RequestScope. This is the.xhtml` file and I do use tommahawk Myfaces:

<?xml version='1.0' encoding='UTF-8' ?>
<!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:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:t="http://myfaces.apache.org/tomahawk"
    xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>Tomahawk file upload demo</title>
    </h:head>
    <h:body>
        <h:form enctype="multipart/form-data">
            <t:inputFileUpload value="#{bean.uploadedFile}" />
            <h:commandButton value="submit" action="#{bean.submit()}" />
            <h:messages />
        </h:form>
    </h:body>
</html>

所以我假设CDI不喜欢战斧库,因为没有默认的构造函数?

So I assume CDI does not like the tomahawk library as there is no default constructor?

推荐答案

查看'开始Java EE 7 它指定CDI 1.1将任何类视为CDI bean,其中包括(除其他事项外):

Looking at 'Begginning Java EE 7' it specifies that CDI 1.1 treats any class as a CDI bean which statifies (amongst other things) :


它有一个没有参数的默认构造函数,没有参数,
或它声明一个带注释的构造函数@Inject。

It has a default constructor with no parameters with no parameters , or it declares a constructor annotated @Inject.

看起来好像是 TomahawkFacesContextFactory 不符合此要求。

It looks as though TomahawkFacesContextFactory doesn't fulfil this requirement.

**编辑**
以上似乎在CDI中确认这里的规格。我已经切换到PrimeFaces,这也与CDI有类似的问题,但我没有在他们的在此公告

** EDIT ** The above seems to be confirmed in the CDI specification here. I've switched to PrimeFaces too and again this has similar problems with CDI however I not on their announcement here that:


PrimeFaces 5.0将附带注释支持的新组件模型,并兼容CDI(或Spring或Guice!)。

PrimeFaces 5.0 will ship with a new component model supported by annotation, and CDI compatible (or Spring or Guice!).

这篇关于Tommahawk Myfaces和CDI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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