JSF selectOneMenu 值未设置
我有一个页面,无论数据库上是否有一些信息,都会呈现 SelectOneMenu。 我的表单看起来像这样:
...
<h:form id="wrapperUpload" enctype="multipart/form-data" >
<h:outputLabel for="option" value="Tipo de carga: "
rendered="#{uploadFile.check(userVerifier.dependencia)}" />
<h:selectOneMenu id="option"
value="#{uploadFile.optionSelected}"
rendered="#{uploadFile.check(userVerifier.dependencia)}" >
<f:selectItems value="#{uploadFile.options}" />
</h:selectOneMenu>
<h:outputLabel for="upfile" value="Archivo: " />
<t:inputFileUpload id="upfile" required="true"
value="#{uploadFile.upFile}" />
<h:commandButton value="Validar #{userVerifier.dependencia}"
action="#{uploadFile.upload}"
onclick="return confirmation()" >
<f:param name="dependencia" value="#{userVerifier.dependencia}" />
</h:commandButton>
</h:form>
...
我的 Beans
private UploadedFile upFile;
private boolean showOptions = false;
private final String[] options = {
"Seleccione una opción.",
"Cargar toda la información.",
"Cargar solo información errónea."
};
private String optionSelected;
private Database db = new Database();
public UploadedFile getUpFile() {
return upFile;
}
public void setUpFile(UploadedFile upFile) {
this.upFile = upFile;
}
public String[] getOptions() {
return options;
}
public void setOptionSelected(String optionSelected) {
this.optionSelected = optionSelected;
}
public String getOptionSelected() {
return optionSelected;
}
public boolean check(String dependencia) {
String hasInfo;
hasInfo = db.checkForInfo(dependencia);
if (hasInfo.equals("T")) {
showOptions = true;
} else {
showOptions = false;
}
return showOptions;
}
public String upload() {
byte[] buffer = null;
int count = 0;
File serverFile = null;
InputStream input = null;
OutputStream output = null;
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String dependencia = params.get("dependencia");
String extension = FilenameUtils.getExtension(upFile.getName());
System.out.println("__depend: " + dependencia);
System.out.println("__option: " + optionSelected); //null
...
...
最后当我点击按钮时 SelectOneMenu (或我的 bean 中的 selectedOption)的值始终为 null... 如何解决这个问题?我错过了什么吗?
忘了提及,如果我删除渲染部分,一切都会正常......
I have a page where a SelectOneMenu is rendered whether there is some info on DB or not.
My form looks like this:
...
<h:form id="wrapperUpload" enctype="multipart/form-data" >
<h:outputLabel for="option" value="Tipo de carga: "
rendered="#{uploadFile.check(userVerifier.dependencia)}" />
<h:selectOneMenu id="option"
value="#{uploadFile.optionSelected}"
rendered="#{uploadFile.check(userVerifier.dependencia)}" >
<f:selectItems value="#{uploadFile.options}" />
</h:selectOneMenu>
<h:outputLabel for="upfile" value="Archivo: " />
<t:inputFileUpload id="upfile" required="true"
value="#{uploadFile.upFile}" />
<h:commandButton value="Validar #{userVerifier.dependencia}"
action="#{uploadFile.upload}"
onclick="return confirmation()" >
<f:param name="dependencia" value="#{userVerifier.dependencia}" />
</h:commandButton>
</h:form>
...
And my Beans is
private UploadedFile upFile;
private boolean showOptions = false;
private final String[] options = {
"Seleccione una opción.",
"Cargar toda la información.",
"Cargar solo información errónea."
};
private String optionSelected;
private Database db = new Database();
public UploadedFile getUpFile() {
return upFile;
}
public void setUpFile(UploadedFile upFile) {
this.upFile = upFile;
}
public String[] getOptions() {
return options;
}
public void setOptionSelected(String optionSelected) {
this.optionSelected = optionSelected;
}
public String getOptionSelected() {
return optionSelected;
}
public boolean check(String dependencia) {
String hasInfo;
hasInfo = db.checkForInfo(dependencia);
if (hasInfo.equals("T")) {
showOptions = true;
} else {
showOptions = false;
}
return showOptions;
}
public String upload() {
byte[] buffer = null;
int count = 0;
File serverFile = null;
InputStream input = null;
OutputStream output = null;
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String dependencia = params.get("dependencia");
String extension = FilenameUtils.getExtension(upFile.getName());
System.out.println("__depend: " + dependencia);
System.out.println("__option: " + optionSelected); //null
...
...
Finally when I hit the button the value of SelectOneMenu (or selectedOption in my bean) is always null...
How to fix this? Am I missing something?
Forgot to mention that, if I delete the render part everything works fine...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这只能意味着该 bean 是请求范围的,并且
rendered
属性取决于基于请求的变量,该变量在显示表单时出现在初始请求中,但在处理表单提交时在后续请求中不存在。将 bean 放入视图范围或确保在后续请求中保留基于请求的变量应该可以解决您的问题。
由于您尚未发布真实的代码,因此无法发布如何正确修复它的代码。
另请参阅:
更新:根据您的更新和评论,您应该使
UserVerifier
成为UploadFile
bean 并在(后)构造函数中进行预初始化。类似于:使用
并摆脱
。That can only mean that the bean is request scoped and that the
rendered
attribute depends on a request based variable which was present in initial request while displaying the form, but is absent in subsequent request while processing the form submit.Putting the bean in the view scope or ensuring that the request based variable is preserved in subsequent request should fix your problem.
As you haven't posted real code, it's not possible to post code of how to fix it properly.
See also:
Update: as per your update and the comments, you should make
UserVerifier
a managed property of theUploadFile
bean and do the preinitialization in (post)constructor. Something like:with
and get rid of
<f:param>
.