0

I've been working with my JSF project and I have a new doubt. My xhtml code looks as follows

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:t="http://myfaces.apache.org/tomahawk">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form enctype="multipart/form-data" >
            <h:selectOneMenu rendered="#{uploadVerifier.check(userVerifier.username)}" >
                <f:selectItems value="#{uploadVerifier.options}" />
            </h:selectOneMenu>
                <br/>
                <t:inputFileUpload required="true" value="#{uploadFile.upFile}" />
                <br/>
            <h:commandButton value="Validar"
                             action="#{uploadFile.upload(userVerifier.username)}"/>
        </h:form>
    </h:body>
</html>

The problem is that, everytime I hit the button it sends me a NullPointerException because it seems that userVerifier.username is not returning a value. This Bean value has a parameter from previous xhtml and even more weird because this value I use it in the h:selectOneMenu ...

What's missing here? Thanks in advance !

P.S. the uploadFile.upload(String param) is supposed to just print the value of param.

BRabbit27
  • 6,333
  • 17
  • 90
  • 161
  • Finally I have a solution. `h:commandButton value="Validar" action="#{uploadFile.upload}"> ` And then in my Bean `Map params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap(); String thing= params.get("truc"); ` And that did it... Now the obvious question is Is this the only way to pass a parameter to an h:commandButton? – BRabbit27 Oct 28 '11 at 15:41
  • I was about to answer your question exactly like you said before I saw this [article](http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/). I think there should be some other problem – Mr.J4mes Oct 28 '11 at 15:48
  • Are you using RequestScope for your userVerifier managed bean? If so, can you try set it to ViewScope and see if it works? – Mr.J4mes Oct 28 '11 at 15:52
  • The article you say is the one I read. I tried using ViewScope (without the f:param) and it didn't work. I think the only way to do that is the way I posted on the first comment. But still I think this should be as easy as passing my userVerifier bean as parameter of another bean. – BRabbit27 Oct 28 '11 at 16:15

1 Answers1

0

The cause is clear: the #{userVerifier.username} is only present in the scope when the page with the form is displayed, not when the form submit is to be processed in the subsequent request. JSF will then namely re-evaluate all EL expressions. If both beans are created in one and same view, then it should work, otherwise you have indeed to pass it as a request parameter instead. I however recommend using <f:viewParam> or @ManagedProperty in the target view/bean instead of manually grabbing it from the request parameter map.

See also:


Unrelated to the concrete problem: you seem to be checking the currently logged-in user in every action. This is clumsy. I suggest to store it in the HTTP session (a session scoped managed bean should do) and create a Filter which checks the presence of the logged-in user on a per-request basis.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555