0

I have a variable in javascript in jsf page and I want to get this variable in my managed bean. have you any idea how I can do it?

alloulou
  • 3
  • 3
  • 1
    There are certain special ways to do this but if you show the exact scenario lurking in your application, there might be completely different ways to achieve this without using a single JavaScript function at all. – Tiny Dec 27 '14 at 18:48

1 Answers1

0

This might not be the best solution, but I have done something similar in the past:

In your managed bean (.java) you can access your front end elements by doing:

Map<String, String> parameters = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();

for (Entry<String, String> formElement : parameters.entrySet()) {
        String elementName = formElement.getKey();
        if (elementName != null && //some other qualifier, for instance if the elementName starts with some pre-defined value) {
            String elementValue = formElement.getValue();
            //Set the variable in the backend

        }
}

And in your .js, you can create an element with for the variable you want to access from your managed bean:

formElement = document.createElement('input');
formElement.type = 'hidden';
formElement.name = someName;
formElement.id = someId;
$(document.getElementById('spanId or divId')).append(formElement);
Denorm
  • 466
  • 4
  • 13