jsf h:inputFile转换器?

时间:2014-07-06 13:23:34

标签: file-upload converter jsf-2.2

我尝试在jsf中为h:inputFile标记创建转换器。发生了什么事情,转换器getAsObject被正确调用,但返回的对象似乎没有在标记中使用。它仍然使用未转换的。

甚至可以为h:inputFile使用转换器?

更新:我使用带有glassfish 4的netbeans 8来运行代码。请求代码示例,所以在这里。

转换器实现 javax.faces.convert.Converter 接口,因为我不知道是否有特殊的文件转换器:

@ManagedBean
@ApplicationScoped
public class ImageConverter implements Converter{

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        System.out.println("ImageConverter getAsObject:" +value); // this is how I know its called
        return new ImageCanvas(); 
      } 

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
         System.out.println("ImageConverter getAsString called: "+value.toString()); // just do it
        return "";
    }


}

我知道 getAsObject 中的参数不会包含文件的内容,但至少要调用该方法,因为转换器 h:inputFile 标记的属性是允许的。 ImageCanvas 类不包含任何魔法,因此不会导致任何问题。

这是jsf页面:

<?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:h="http://java.sun.com/jsf/html"
      xmlns:comp="http://java.sun.com/jsf/composite/components">
    <h:body>
        <h:form enctype="multipart/form-data" >
            <h:inputFile value="#{submitAsImageCanvas.image}" converter="#{imageConverter}"/>
            <h:commandButton  value="submit" action="#{submitAsImageCanvas.submit()}" />
        </h:form>
    </h:body>
</html> 

这是viewScoped bean,它希望 image 属性为 ImageCanvas

@Named
@ViewScoped
public class SubmitAsImageCanvas implements Serializable{

    private ImageCanvas image;

    public ImageCanvas getImage() {
        return image;
    }

    public void setImage(ImageCanvas image) {
        this.image = image;
    }

    public void submit() {
        System.out.println("Submit called");
    }

}

当我请求jsf页面然后提交文件时,这就是netbeans输出的内容:

FATAL:   JSF1073: javax.faces.component.UpdateModelException caught during processing of     UPDATE_MODEL_VALUES 4 : UIComponent-ClientId=j_idt3-j_idt4, Message=javax.el.ELException: /uploadConverterImageCanvas.xhtml @8,94 value="#{submitAsImageCanvas.image}": java.lang.IllegalArgumentException: Cannot convert File name=test.txt, StoreLocation=%USERPATH%\AppData\Roaming\NetBeans\8.0\config\GF_4.0\domain1\generated\jsp\AdApp\upload__6fff2383_14717f72869__7ff1_00000200.tmp, size=4bytes, isFormField=false, FieldName=j_idt3-j_idt4 of type class org.apache.catalina.fileupload.PartItem to class model.ad.ImageCanvas
FATAL:   javax.el.ELException: /uploadConverterImageCanvas.xhtml @8,94 value="#{submitAsImageCanvas.image}": java.lang.IllegalArgumentException: Cannot convert File name=test.txt, StoreLocation=%USERPATH%\AppData\Roaming\NetBeans\8.0\config\GF_4.0\domain1\generated\jsp\AdApp\upload__6fff2383_14717f72869__7ff1_00000200.tmp, size=4bytes, isFormField=false, FieldName=j_idt3-j_idt4 of type class org.apache.catalina.fileupload.PartItem to class model.ad.ImageCanvas
javax.faces.component.UpdateModelException: javax.el.ELException: /uploadConverterImageCanvas.xhtml @8,94 value="#{submitAsImageCanvas.image}": java.lang.IllegalArgumentException: Cannot convert File name=test.txt, StoreLocation=%USERPATH%\AppData\Roaming\NetBeans\8.0\config\GF_4.0\domain1\generated\jsp\AdApp\upload__6fff2383_14717f72869__7ff1_00000200.tmp, size=4bytes, isFormField=false, FieldName=j_idt3-j_idt4 of type class org.apache.catalina.fileupload.PartItem to class model.ad.ImageCanvas
    at javax.faces.component.UIInput.updateModel(UIInput.java:867)
    at javax.faces.component.UIInput.processUpdates(UIInput.java:749)
    at javax.faces.component.UIForm.processUpdates(UIForm.java:281)
    at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1286)
    at javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1286)
    at javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1254)
    at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:78)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.el.ELException: /uploadConverterImageCanvas.xhtml @8,94 value="#{submitAsImageCanvas.image}": java.lang.IllegalArgumentException: Cannot convert File name=test.txt, StoreLocation=%USERPATH%\AppData\Roaming\NetBeans\8.0\config\GF_4.0\domain1\generated\jsp\AdApp\upload__6fff2383_14717f72869__7ff1_00000200.tmp, size=4bytes, isFormField=false, FieldName=j_idt3-j_idt4 of type class org.apache.catalina.fileupload.PartItem to class model.ad.ImageCanvas
    at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:139)
    at javax.faces.component.UIInput.updateModel(UIInput.java:832)
    ... 36 more
Caused by: javax.el.ELException: java.lang.IllegalArgumentException: Cannot convert File name=test.txt, StoreLocation=%USERPATH%\AppData\Roaming\NetBeans\8.0\config\GF_4.0\domain1\generated\jsp\AdApp\upload__6fff2383_14717f72869__7ff1_00000200.tmp, size=4bytes, isFormField=false, FieldName=j_idt3-j_idt4 of type class org.apache.catalina.fileupload.PartItem to class model.ad.ImageCanvas
    at com.sun.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java:79)
    at javax.el.ELContext.convertToType(ELContext.java:473)
    at com.sun.el.lang.EvaluationContext.convertToType(EvaluationContext.java:166)
    at com.sun.el.parser.AstValue.setValue(AstValue.java:235)
    at com.sun.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:294)
    at org.jboss.weld.el.WeldValueExpression.setValue(WeldValueExpression.java:64)
    at com.sun.faces.facelets.el.TagValueExpression.setValue(TagValueExpression.java:131)
    ... 37 more
Caused by: java.lang.IllegalArgumentException: Cannot convert File name=test.txt,     StoreLocation=%USERPATH%\AppData\Roaming\NetBeans\8.0\config\GF_4.0\domain1\generated\jsp\AdApp\upload__6fff2383_14717f72869__7ff1_00000200.tmp, size=4bytes, isFormField=false, FieldName=j_idt3-j_idt4 of type class org.apache.catalina.fileupload.PartItem to class model.ad.ImageCanvas
    at com.sun.el.lang.ELSupport.coerceToType(ELSupport.java:417)
    at com.sun.el.ExpressionFactoryImpl.coerceToType(ExpressionFactoryImpl.java:77)
    ... 43 more

换句话说,它是否告诉我它没有尝试使用我的转换器,因为转换器 getAsObject 方法从未被调用(因为没有调用系统打印)?

所以我尝试简单地通过将 ImageCanvas 中的属性类图像更改为bean SubmitAsImageCanvas中的 Part 来回答这个问题

@Named
@ViewScoped
public class SubmitAsImageCanvas implements Serializable{

    private Part image;

    public Part getImage() {
        return image;
    }

    public void setImage(Part image) {
        this.image = image;
    }

    public void submit() {
        System.out.println("Submit called");
    }

}

所以这就是netbeans:

Info:   Submit called
Info:   ImageConverter getAsString called: File name=test.txt, StoreLocation=%USERPATH%\AppData\Roaming\NetBeans\8.0\config\GF_4.0\domain1\generated\jsp\AdApp\upload__6fff2383_14717f72869__7ff1_00000204.tmp, size=4bytes, isFormField=false, FieldName=j_idt3-j_idt4

好的,没有异常和 getAsObject 没有被调用,因此它永远不会尝试将其转换为对象。提交后页面刷新和 getAsString 被调用,以便将标记的属性赋予字符串对吗?

为什么我要将转换器与文件一起使用?我不想在视图bean setter / getters中进行转换,因为转换器会分割代码并使这些bean干净简单。我也希望&#34;转换&#34;将文件转换为图像。 Part 对象不可序列化(?),我将使用可能发送到远程bean的小文件。

回到我的问题,转换器是否支持文件?在扩展课程时,这只是一个漫长的过程吗?为什么有一个转换器属于FILEINPUT的属性?

0 个答案:

没有答案