rLyLmZ
rLyLmZ

Reputation: 495

How to pass a value inside javascript to managed bean property without using hidden tags in JSF?

we wonder that if it is possible to get managed bean property value inside the javascript method in facelets or pass a javascript value to managed bean property but without using hidden tags?

When we search about them what we see are all about the examples or solutions that use html hidden tags or hidden button's click events. But this method is not useful for us when we need much data exchange between managedbean and javascript as it needs lots of hidden tags.

Upvotes: 2

Views: 4128

Answers (1)

Srikanth Ganji
Srikanth Ganji

Reputation: 1157

You can create a json object with name value pair like in a map and send them as a request parameters. You can have a p:remoteCommand which could be called from your javascript function like below

function sendParams() {
    passToJSFManagedBean ([ {
                  name : 'sno',
                  value : 1
                 },   
                 {
                   name : 'name',
                   value : srikanth
                 }  
               ]);   
} 

The above passToJSFManagedBean should be a name of a remote command function like below

 <p:remoteCommand name="passToJSFManagedBean" id="passToJSFManagedBeancmd"
                action="#{myBean.getParams}"
                process="@this" />   

You can access the params passed in your managed bean action

   public void getParams() {
        String sno= FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()  
           .get("sno");    
 //same way you can get name
   }  

Hope this helps

Upvotes: 4

Related Questions