1

I have been searching for an answer, but have not found one...

I want to pass variable value FROM javascript TO bean. Is it possible to do that without going into xhtml? I guess i'm just stupid, but I have not found an answer to this. Can somebody give me a real dummy guide for a real dummies? :)

Let's say, I want to send javascript variable X value to named Bean "user" and in bean I have getter and setter for a string.

Dummy-guide, please. I just don't seem to get this... Old dog and new tricks, you know :D

If it has to be done thru XHTML, I want to use p:remotecommand.


EDIT: QUOTE: "This question may already have an answer here:"

Maybe so, but I still don't understand it. I'm just that stupid I guess... :(

finjay
  • 15
  • 1
  • 6
  • Perhaps you can define invisible input field and fill it by javascript with required values and then call to p:remoteCommand to process and update this field. But don't use rendered = false to make field invisible, you won't be able to process it. Didn't try it, but think it might work. – Anatoly Nov 10 '14 at 18:23

1 Answers1

1

Anatoly is right: Primefaces' <p:remoteCommand> is perfectly fine for this.

Declare the remoteCommand anywhere inside your xhtml page, for example:

<p:remoteCommand name="SetMyStrings" actionListener="#{myBean.setMyStrings}" />

from within your Javascript you can now invoke the remoteCommand, using

SetMyStrings([{ name: "string1", value: "Hello"}, {name="string2", value: "World"}]);

(or ofc. obtain the string values from your javascript variables rather than hardcoding them)

Finally, all you need to do inside the backing bean is collect the values from the RequestParameter Map:

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

   String string1 = requestParamMap.get("string1"); //Hello
   String string2 = requestParamMap.get("string2"); //World
}
Kawu
  • 13,647
  • 34
  • 123
  • 195
dognose
  • 20,360
  • 9
  • 61
  • 107
  • In my Bean, do I have to use Map? If I just want set one String, can I use that Strings SETTER? How would I do that. I tried it but it's not working. – finjay Nov 11 '14 at 07:42