Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

primefaces remoteCommand tag is not working when Passing html of my current page got through Jquery to managed bean

I am getting my current page HTML using Jquery's .html() method like this.

<h:outputScript name="js/jquery-1.7.2.js" />
<h:outputScript>
   function getHtml(){
        $('#next').click(function(){  
           htmlString=$('#wrapper').html();
           alert(htmlString);
       command({param:htmlString});
          });
        }
</h:outputScript>

My XHTML page

<div id="wrapper">
 <form prependId="false">
 // My HTML form with some input fields
<h:commandButton id="next" value="Submit" action=#{bean.formvalues} onCick="getHtml();">
    <h:commandButton>
<p:remoteCommand name="command" actionListener="#{bean.getjs}" />
</form>
</div>

My Bean

    @ManagedBean
    @sessionScoped
    public class bean{

       private String formvalues; // getters and settes

      public String getformValues(){
       String htmlString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("htmlString");
    return htmlString;
    }
}

public void getjs(){
     String value = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("param");
          System.out.println("**************** The Javascirpt is "+value);
    }

But I am not able to get the "htmlString" which has my page HTML source in the bean when I use this primefaces remoteCommand tag.How can get it into the bean.

like image 729
mdp Avatar asked Feb 18 '23 20:02

mdp


1 Answers

You should call remoteCommand like this;

<h:outputScript>
   function getHtml(){
       $('#next').click(function(){  
           htmlString=$('#wrapper').html();
           alert(htmlString);
           command([{name:'param',value:htmlString}]); //This is important
       });
    }
</h:outputScript>   

and you can get value of param in getJs method:

public void getjs(){
  FacesContext context = FacesContext.getCurrentInstance();
  Map<String, String> map = context.getExternalContext().getRequestParameterMap();
  String value = (String) map.get("param");
  System.out.println("**************** The Javascript is " + value);
}
like image 129
mstzn Avatar answered Feb 27 '23 12:02

mstzn