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

若如初见. 提交于 2019-12-04 01:53:00

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/25258740/how-to-pass-a-value-inside-javascript-to-managed-bean-property-without-using-hid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!