java JSF 在视图根中找不到具有表单 ID 的组件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2580837/
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
JSF don't find component in view root with the form id
提问by kenzokujpn
I have a t:inputFileUpload inside the form, in the html of the display page the id of this component is form:inputFile but when I tried to get the component from the view root using "form:inputFile" the return is null, but when the "form:" is removed the return is the component. The component don't set the value in my managed bean, someone have this problem?
我在表单中有 at:inputFileUpload,在显示页面的 html 中,该组件的 id 是 form:inputFile 但是当我尝试使用“form:inputFile”从视图根获取组件时,返回为空,但是当“形式:”被删除返回是组件。组件没有在我的托管 bean 中设置值,有人有这个问题吗?
EDIT:
编辑:
<h:form id="form" enctype="multipart/form-data">
<t:inputFileUpload id="inputFile" size="40" value="#{managedBean.inputFile}"/>
</h:form>
In the managed bean:
在托管 bean 中:
private UploadedFile inputFile;
with the gets and sets provided by Eclipse.
使用 Eclipse 提供的获取和设置。
//This method scans the view root and returns the component with the id passed as parameter
findComponentInRoot("form:inputFile");
This returns null, but when I use:
这将返回 null,但是当我使用时:
//This method scans the view root and returns the component with the id passed as parameter
findComponentInRoot("inputFile");
The return is the component I'm looking for, but when I use the View Source in Internet Explorer the id of this component is "form:inputFile".
返回的是我正在寻找的组件,但是当我在 Internet Explorer 中使用查看源时,该组件的 ID 是“form:inputFile”。
I don't know if this is related, but the component don't set the value in my managed bean and it's strange the fact that the id of the component is different from the HTML source. I'm using JSF 1.2 Mojarra. Someone else has this problem? Or know why this happens?
我不知道这是否相关,但是组件没有在我的托管 bean 中设置值,而且组件的 id 与 HTML 源不同,这很奇怪。我正在使用 JSF 1.2 Mojarra。其他人有这个问题吗?或者知道为什么会这样?
EDIT2: Okay, I'm very stupid, aparently the build wasn't working correctly and when the build was changed to other task from the Ant it worked (still don't know why, but simply worked). Sorry for the trouble.
EDIT2:好吧,我很愚蠢,显然构建没有正常工作,当构建从 Ant 更改为其他任务时,它工作了(仍然不知道为什么,但只是工作)。抱歉,添麻烦了。
回答by BalusC
You should use component bindingor UIViewRoot#findComponent(). But that won't solve the problem of the uploaded file not being set. To fix it, first step is to ensure that you definied and configured the ExtensionsFilterproperly as per the Tomahawk documentation, since it is the one responsible for parsing the multipart/form-datarequest and putting all the parameters among with the uploaded file back in the request parameter map, so that the FacesServletcan apply them and update the model values.
您应该使用组件binding或UIViewRoot#findComponent(). 但这并不能解决上传文件未设置的问题。要修复它,第一步是确保您ExtensionsFilter按照Tomahawk 文档正确定义和配置,因为它负责解析multipart/form-data请求并将所有参数与上传的文件放回请求参数映射中,所以该FacesServlet可以应用它们,更新模型值。
回答by Bozho
I guess findComponentInRootis this(a minor detail you should've shared).
我想findComponentInRoot是这个(你应该分享的一个小细节)。
Anyway, using findComponent(..)or getChildren(..)always return the idof the components as defined in the page. The html id is something different that consists of the naming container:id.
无论如何,使用findComponent(..)或getChildren(..)始终返回id页面中定义的组件。html id 是不同的东西,由naming container:id.

