4

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.

mdp
  • 815
  • 4
  • 17
  • 32
  • if you use primefaces,you can try with remoteCommand..http://www.primefaces.org/showcase-labs/ui/remoteCommand.jsf – mstzn Oct 16 '12 at 10:22
  • Hey can you see my Updated question ? It's not working for me any Ideas. – mdp Oct 16 '12 at 17:58

1 Answers1

8

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);
}
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53
mstzn
  • 2,881
  • 3
  • 25
  • 37