gpt4 book ai didi

java - 当我当前页面的 html 通过 Jquery 传递到托管 bean 时,primefaces remoteCommand 标记不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 01:45:15 24 4
gpt4 key购买 nike

我正在使用 Jquery 的 .html() 方法获取当前页面的 HTML。

<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>

我的 XHTML 页面

<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>

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

但是当我使用这个 primefaces remoteCommand 标签时,我无法在 bean 中获取包含我的页面 HTML 源代码的 "htmlString"。如何将它放入 bean 中。

最佳答案

你应该这样调用remoteCommand;

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

在getJs方法中可以得到param的值:

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

关于java - 当我当前页面的 html 通过 Jquery 传递到托管 bean 时,primefaces remoteCommand 标记不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12908693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com