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

a 夏天 提交于 2019-12-12 12:05:21

问题


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.


回答1:


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);
}


来源:https://stackoverflow.com/questions/12908693/primefaces-remotecommand-tag-is-not-working-when-passing-html-of-my-current-page

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