Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top