PrimeFace+JSF实际工作需要点击一个树将关联的文本添加到inputTextarea文本框,inputTextarea允许用户随意更改,ajax绑定树的select事件,但点击时,后台Bean对象无法实时获取前台文本框改变的实际内容。
原因估计ajax控件无法将前台文本框数据关联到后台。
解决方案:
添加一个按钮和文本框鼠标事件,事件JS调用按钮click事件,由按钮将文件夹数据同步到后台。
前台代码:
<script>
function callButton(){
document.getElementById("first:tj").click();
}
</script>
<h:form>
<h:inputHidden value="#{inputRule.init}"/>
</h:form>
<h:form id="first">
<h:panelGrid>
<p:inputTextarea rows="5" cols="50" id="bigout" value="#{inputRule.text}" onmouseout="callButton()">
</p:inputTextarea>
<p:tree value="#{inputRule.sgnrRoot}" var="node" selection="#{inputRule.sgnrNode}" selectionMode="single" style="width:215px;height:150px;border:0;">
<p:treeNode expandedIcon="ui-icon-folder-open" collapsedIcon="ui-icon-folder- collapsed">
<h:outputText value="#{node}" style="display:none;" />
<h:outputText value="#{node}" />
</p:treeNode>
<p:ajax event="select" listener="#{inputRule.addInputString}" update="@form"/>
</p:tree>
<p:commandButton value="提交" id="tj" style="display:none">
</p:commandButton>
</h:panelGrid>
</h:form>后台代码:
@ManagedBean(name = "inputRule")
@SessionScoped
public class InputRule implements Serializable {
// 施工常用语tree操作定义区
private TreeNode sgnrRoot;// 施工内容root
private TreeNode sgnrNode;// 选择某条内容常用语
private List<String> nrcyyList = new ArrayList<String>();// 内容常用语所有对象
public String getInit(){
sgnrRoot = new DefaultTreeNode("Root", null);
nrcyyList.clear();
nrcyyList.add("a");
for (String str : nrcyyList) {
TreeNode sgxm = new DefaultTreeNode(str, sgnrRoot);
}
return null;
}
private String text="";
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public void addInputString(){
text += "测试1,";
}
public void getInputString(){
String a = text;
}
}PrimeFace JSF中使用ajax控件无法实时更新文本框inputTextarea内容
原文:http://116833.blog.51cto.com/106833/1689167