3

I am struggling with a JSF project. In my service, I retrieve a List of custom object (here Sales), and I have to pass it to my jsf view, specifically into the javascript, to make graphs.

My problem is, I don't understand how to send the data from the controller (my managed beans) to my view, and which tag to use in my view to retrieve it in my javascript.

I think I can pass my data like this, but I'm not sure

public String passData() {
    List<Sales> bestSelling = saleService.getBestSellingProduct(null, null, null, null);
    List<Sales> worstSelling = saleService.getWorstSellingProduct(null, null, null, null);

    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("bestSelling", bestSelling);
    FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("worstSelling", worstSelling);

    return "./all.jsf?faces-redirect=true";
}
5
  • You're using completely the wrong tool for the job. JSF is a component based MVC framework, not a web service framework. Look at JAX-WS/JAX-RS. Commented May 15, 2013 at 11:09
  • Well, I just want to pass the result from the request in my DB to my view and make a chart. So I made a service, I get the List of result in my managed beans and try to pass it in the view. You can't do that with a MVC Framework ? Commented May 15, 2013 at 11:14
  • the C of the MVC is in this particular case in JavaScript side (the V being the HTML DOM tree). Basically, you're using JSF as being a whole MVC framework for the M part. This makes no sense. The answer of Skuntsel basically lets JSF print the desired data in valid JavaScript syntax when the JSF page is requested (it does however not pass the data to JavaScript as ajax response or so as you seem to be attempting). Commented May 15, 2013 at 11:15
  • Hmm, ok. Well, I don't have the choice in the tool so … Have to work with what I got. I'm not trying to get this in ajax, just when I load the page. Very basic in fact. At the load of the page, it displays graph that used data from the db. Commented May 15, 2013 at 11:20
  • 1
    Then the answer of Skuntsel applies and you need to revise your question. Please keep in mind that JSF is in the context of this question merely a HTML/CSS/JS code generator. There's no means of "passing" data around from JSF to JS. There's only means of "printing" data (in JS syntax). Rightclick and View Source in browser to get enlightenend. All you need to make sure is that JSF generates that HTML/CSS/JS code in desired and valid syntax. Commented May 15, 2013 at 11:21

1 Answer 1

3

You need to create a JSON object that you'll assign to a JavaScript variable. To create JSON you may find useful to incorporate a library like Gson.

So, it'll look like:

var sales = #{bean.jsonList};

with Bean#getJsonList as:

public String getJsonList() {
     return (sales == null) ? "" : new Gson.toJson(sales);
}

Just don't forget that the script with such assignment must be handled by the FacesServlet.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for that ! What do you mean it "must be handled by FacesServlet" ? I can't do it in my managedbeans ? I'm very new to JSF, I'm still trying to understand it …
That means that the EL expression must be evaluated, thus must go through the FacesServlet, that handles creation of HTML markup based on JSF pages and, in particular, evaluation of EL expressions. Just let this small piece of JS reside inside a JSF page, or access the script with <h:outputScript>.
So I have to pass "sales" inside <h:outputScript> before evaluating it ?
The assignment itself must be inside a JS file, and you need to include it in a JSF page with <h:outputScript>, and not plain <script> tag.
so, if I get it right, I use var sales = #{bean.jsonList}; in a separate js file, and include this file with <h:outputScript>. Am I rigth ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.