JSF selectOneMenu值未设置

时间:2011-11-03 16:09:13

标签: jsf selectonemenu

我有一个页面,其中呈现SelectOneMenu是否有关于DB的某些信息。 我的表格如下:

...
<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>
...

我的豆子是

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 ... 如何解决这个问题?我错过了什么吗?

忘了提一下,如果我删除渲染部分,一切正常......

1 个答案:

答案 0 :(得分:0)

  

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

这只能表示bean是请求作用域,并且rendered属性依赖于在显示表单时初始请求中存在的基于请求的变量,但在处理表单提交时后续请求中不存在

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

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

另见:


更新:根据您的更新和评论,您应该使UserVerifier成为UploadFile bean的托管属性,并在(post)构造函数中执行预初始化。类似的东西:

@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}"

并摆脱<f:param>