JSF selectOneMenu 值未设置

发布于 2024-12-13 09:55:39 字数 2566 浏览 1 评论 0原文

我有一个页面,无论数据库上是否有一些信息,都会呈现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此岸叶落 2024-12-20 09:55:39

忘记提及,如果我删除渲染部分,一切都会正常

这只能意味着该 bean 是请求范围的,并且 rendered 属性取决于基于请求的变量,该变量在显示表单时出现在初始请求中,但在处理表单提交时在后续请求中不存在。

将 bean 放入视图范围或确保在后续请求中保留基于请求的变量应该可以解决您的问题。

由于您尚未发布真实的代码,因此无法发布如何正确修复它的代码。

另请参阅:


更新:根据您的更新和评论,您应该使 UserVerifier 成为UploadFile bean 并在(后)构造函数中进行预初始化。类似于:

@ManagedBean
@ViewScoped
public class UploadFile {

    @ManagedProperty("#{userVerifier}")
    private UserVerifier userVerifier; // +setter

    private boolean showOptions;

    @PostConstruct
    public void init() {
        showOptions = "T".equals(db.checkForInfo(userVerifier.getDependencia()));
    }

    public boolean isShowOptions() {
        return showOptions;
    }

    // ...
}

使用

rendered="#{uploadFile.showOptions}"

并摆脱

Forgot to mention that, if I delete the render part everything works fine

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 the UploadFile bean and do the preinitialization in (post)constructor. Something like:

@ManagedBean
@ViewScoped
public class UploadFile {

    @ManagedProperty("#{userVerifier}")
    private UserVerifier userVerifier; // +setter

    private boolean showOptions;

    @PostConstruct
    public void init() {
        showOptions = "T".equals(db.checkForInfo(userVerifier.getDependencia()));
    }

    public boolean isShowOptions() {
        return showOptions;
    }

    // ...
}

with

rendered="#{uploadFile.showOptions}"

and get rid of <f:param>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文