@Named注释不起作用

时间:2013-12-21 00:00:03

标签: java-ee annotations cdi

我在使用@Named注释时遇到了一些问题。例如,以下bean正常工作,它接收并上传文件:

@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@RequestedScope替换为@Named@RequestScoped,则会因此异常而失败:

  

javax.el.PropertyNotFoundException:目标无法访问,标识符'bean'已解析为null

我知道@Named我的Netbeans项目中是否设置了不正确的版本或类似的内容?我也在Glassfish 4服务器上部署。

由于

1 个答案:

答案 0 :(得分:0)

CDI 1.1是否需要beans.xml