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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With