【问题标题】:Update the page with command bouton primefaces使用命令 bouton primefaces 更新页面
【发布时间】:2013-04-25 12:48:23
【问题描述】:

我想获取输入 Textarea 的值,并通过单击命令按钮“编译器”将其显示在同一页面中 但是我没有得到任何结果!并且仅当我使用浏览器更新程序更新时才显示 contnet Sh 我如何更新页面和托管 bean 以在同一页面中显示 primefaces 文本区域的内容 这是代码:

<p:layoutUnit position="west" size="520" header="Code à executer" resizable="true" >
    <h:form id="moncode">
 <p:inputTextarea id="mycode" value="#{fichier.code}" rows="17" cols="50" /> 
 <p:commandButton value="compiler" id="btn3"  action="guest.xhtml"   styleClass="myButtonClass" />
    </h:form>
</p:layoutUnit>

<p:layoutUnit position="east" size="550" header="Resultat d'execution" resizable="true" >
   <h:outputText value="#{fichier.code}" /> 
</p:layoutUnit>


             <p:layoutUnit position="south"  >




我的应用程序是关于编译给定代码:我编写了一个代码,然后我使用“编译器”按钮执行,因此将创建一个文件,但是该文件始终使用“null”创建,我认为是因为 var“code " 尚未在托管 bean 中设置,为什么我要更新页面,所以这里设置的托管 bean 是编译: ` 私有字符串代码; 私有字符串错误;

public String getCode() {
    return code;
}

public String getError() {
    return error;
}
 public void setCode(String code) {
    this.code = code;
}

 public void compile() throws IOException {
 File file = new File("C:\\Users\\Rad1\\test.c");
 PrintWriter ecrivain;  
 ecrivain = new PrintWriter(new BufferedWriter (new FileWriter(file))); 
      ecrivain.println(code);  
  ecrivain.close();                

`

【问题讨论】:

  • 更清楚地说明您的问题/要求。什么没有按您的预期工作?你的意图是什么?有什么错误?

标签: html jsf jsf-2 primefaces


【解决方案1】:

有两种方法可以实现您想要的效果:使用同步提交表单和后续回发,或发送 AJAX 请求以部分更新页面的必要部分。

同步提交

<p:inputTextarea id="mycode" value="#{fichier.code}" rows="17" cols="50" /> 
<p:commandButton value="compiler" id="btn3" action="#{fichier.action}" styleClass="myButtonClass" ajax="false" />
<h:outputText id="upd" value="#{fichier.code}" /> 

带有动作方法

public String action() {
    //do business job
    return null;
}

通过这种方式,将通过回发来刷新字段。不要忘记 bean Fichier 必须是视图范围的。注意&lt;p:commandButton&gt;ajax="false" 属性。

AJAX 调用

<p:inputTextarea id="mycode" value="#{fichier.code}" rows="17" cols="50" /> 
<p:commandButton value="compiler" id="btn3" action="#{fichier.action}" styleClass="myButtonClass" update="upd" />
<h:outputText id="upd" value="#{fichier.code}" /> 

用同样的动作方法

public String action() {
    //do business job
    return null;
}

这样只有&lt;h:outputText&gt; 的内容会在AJAX 调用完成后更新。不要忘记 bean Fichier 也应该是视图范围的。注意&lt;h:outputText&gt;id属性必须指定。

【讨论】:

  • 谢谢@skuntsel,但它并没有放弃我想要的,我已经编辑了我的帖子以清除它
  • 我不得不添加另一个按钮来刷新输入文本,以便在托管 bean 中设置它,然后在编译函数中使用它
【解决方案2】:

实际上,您不会调用操作本身。您的操作转发到某个页面。在设置属性和操作本身之间的 JSF 操作中不存在竞争条件或同步问题。我建议您阅读JSF life cycles tutorial by BalusCAplly 请求值阶段运行前的操作。

尝试在命令按钮上调用操作。

<p:commandButton value="compiler" id="btn3"  action="#{fichier.compile}" update="outputCode"  styleClass="myButtonClass" />

在您的输出面板中添加一个 id 属性并在操作按钮中指定它。

<p:layoutUnit id="outputCode" position="east" size="550" header="Resultat d'execution" resizable="true" >
   <h:outputText value="#{fichier.code}" /> 
</p:layoutUnit>

附:阅读this BalusC answer 以更好地了解操作、Ajax/非 Ajax 请求等也很好。

【讨论】:

  • OP 的代码会进行转发,而不是重定向。如果你仔细观察,它是一个 AJAX 命令按钮。
  • 好的。我的代码也执行 ajax 请求,我将“重定向”字词编辑为“转发”
猜你喜欢
  • 2013-07-06
  • 2014-04-16
  • 2011-11-10
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
  • 2011-12-31
相关资源
最近更新 更多