0

I'm getting a weird trouble trying to extract one request parameter into a Managed Bean. I'm Using a javascript method to extract browser info (horizontal and vertical dimensions). This method uses a XMLHttpRequest to send a GET request to deliver a parameter with my desired browser information to server side. All this works ok, but when I try to read parameter value inside a managed bean I'm always getting a NULL value.

I tried to read parameter before and after page load, without success. What is very weird is that I'm using FIREBUG to show request processing and this parameter (workArea) is shown with correct value as seen in image below.

FIREBUG PrintScreen

This is managed bean code ....

    public void getBrowserWorkArea()
    {
          Map<String,String> requestParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();

          String selectIDRastreador = requestParams.get("workArea");

          localMapResolutionH = selectIDRastreador;

          if (selectIDRastreador != null)
          {
              int paramIndex = localMapResolutionH.indexOf("px");

              localMapResolutionH = selectIDRastreador.substring(0,paramIndex + 2);
              localMapResolutionV = selectIDRastreador.substring(paramIndex+2,selectIDRastreador.length());
              mainPanelGridStyle = "text-align:center; width: " + localMapResolutionH;

              FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().put("localMapResolutionH", localMapResolutionH);
              FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().put("localMapResolutionV", localMapResolutionV);

              FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().put("mainPanelGridStyle",mainPanelGridStyle );

          }
          else
              mainPanelGridStyle = "text-align:center; width: 1050px ";

      }

Just to make things more clear, this is a session scoped bean, with this behavior configured at faces-config.xml.

This is xhtml code:

<html xmlns="http://www.w3c.org/1999/xhtml"
  xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:ui="http://java.sun.com/jsf/facelets">

    <h:head>
        <link type="text/css" rel="stylesheet" href="#{request.contextPath}/resources/css/alinhamento.css" />    

        <h:outputScript library="javascript" name="cliente_ci.js" target="head" /> 
    </h:head>

    <h:body>

       <h:form id="formLogin" class="algnCentralizado">

         <div class="algnCentralizado">
            <br></br>

            <br></br>                     
            <br></br>      
            <br></br>     
            <br></br>
               Login <p:inputText id="user_login" value="*****" label="Senha">

            </p:inputText>

            <br></br> 
            <br></br>
               Senha <p:inputText id="user_passwd" value="*****" label="Senha">

            </p:inputText>

            <br></br> 
            <br></br>
            <p:commandButton                
               icon   = "ui-icon-check"        styleClass="centered"  
               action = "#{BeanLogin.carregaInicial}"
               value  = "Ok" ajax="false"  float="center"/>
            <br></br>

            <f:view>
               <h:outputText value="#{BeanLogin.mainPanelGridStyle}" />
            </f:view>

          </div>

        </h:form>

        <h:outputScript>
           setBrowserWorkArea();
        </h:outputScript>

    </h:body>
</html>

Below is my JavaScript function responsible to perform GET request. It's called from xhtml page using h:outputScript :

       function setBrowserWorkArea()
       {
          var http = new XMLHttpRequest();
          var url = "http://localhost:8080/europa_tracker?";

          var browserW = window.innerWidth;
          var browserH = window.innerHeight;
          var params =  "workArea=";
          params     =  params.concat(browserW);
          params     =  params.concat("px");
          params     =  params.concat(browserH);
          params     =  params.concat("px");

          http.open("GET", url + params, true);
          http.send();

       }

QUESTION: Why I'm getting parameter value NULL inside managed bean? There is any way to ensure that data is arriving correctly at application server? THANKS!

SiriusB
  • 21
  • 1
  • 3
  • Which JSF implementation version? – Aritz May 12 '14 at 15:25
  • Don't need either to use faces config file or extracting the parameter from the context. Everything is much easier than that in JSF 2. Have a look to any starter tutorial. – Aritz May 14 '14 at 05:06
  • I'm aware about that faces-config.xml can (and I'll do it later) be thrown away in favor of annotations. But you have any sugestion on how to send some kind of data about client browser to server side? – SiriusB May 14 '14 at 05:30
  • http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/ – Aritz May 14 '14 at 08:46
  • Thank you for your help, Xtreme Biker. In all examples at [http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean] JSF is used to handle user inputs, something it does natively very well. What I need is send to server user's browser information. This data are present at client side and I don't know (I'm not saying that it doesn't exist) means other than javascript to catch this data. – SiriusB May 14 '14 at 19:07
  • Well, I completelly misunderstood your issue, sorry about that. Actually, you're missing the important details in your question: What does `setBrowserWorkArea()` JS function do and where is `getBrowserWorkArea()` being called from? Just as a tip: follow the [SSCCE](http://www.sscce.org/) format and post your question in a way I can copy-paste and reproduce it. I'll be then able to offer you further help. Have a look to [some of my questions](http://stackoverflow.com/questions/19591366/two-primefaces-calendar-component-validation) to get an idea! – Aritz May 15 '14 at 08:47
  • Thank you for you help again Xtreme Biker . I did some editions in my question in order to try to comply with SSCCE tip. setBrowserWorkArea() is a JavaScript function responsible to perform a GET request. It is called from xhtml page using and is used to send a parameter that concatenates browser's horizontal and vertical resolution. I'll adopt 2 separate parameters after solving NULL problem in order to have a clean code. Now it's done in this way only for debug purposes. Thanks! – SiriusB May 16 '14 at 00:43

0 Answers0