【发布时间】:2014-02-24 10:57:23
【问题描述】:
我在将 p:remoteCommand 添加到表单时遇到问题。它看起来像:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
xmlns:util="http://java.sun.com/jsf/composite/components/util"
xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Reset Test</title>
<link type="text/css" rel="stylesheet" href="/treetable-sscce/css/example.css" />
<h:outputScript library="primefaces" name="jquery/jquery.js"/>
</h:head>
<div class="box">
<h2>Box</h2>
<h:panelGroup id="mypanel">
Headline: <h:outputText value="#{resetBean.headline}" />
<br/>
Message : <h:outputText value="#{resetBean.message}" />
<br/>
</h:panelGroup>
</div>
<div class="box">
<h2>Form</h2>
<h:form id="myform" acceptcharset="utf-8">
<p:growl id="growl" showDetail="true" sticky="false" severity="info, warn" />
<!-- register custom validate event -->
<f:event listener="#{resetBean.validateForm}" type="postValidate" />
<p:remoteCommand name="resetByEscape" action="#{resetBean.resetAction}"
immediate="true" update=":myform :mypanel" />
<h:outputLabel for="headline">Meldungsüberschrift</h:outputLabel>
<h:inputText id="headline" value="#{resetBean.headline}" />
<br/>
<h:outputLabel for="message">Meldungsüberschrift</h:outputLabel>
<h:inputTextarea id="message" value="#{resetBean.message}" />
<br/>
<h:commandButton action="#{resetBean.resetAction}"
value="Reset" immediate="true" onclick="resetForm()"/>
<h:commandButton action="#{resetBean.submitAction}" value="Submit" immediate="false"/>
</h:form>
</div>
<script type="text/javascript">
<!--//--><![CDATA[//><!--
var resetForm = function()
{
$("[id$='headline']").val(null)
$("[id$='message']").val(null)
}
var escapePressed = function()
{
resetForm();
resetByEscape();
}
$(document).keyup(function(e) {if (e.keyCode == 27) escapePressed();});
//--><!]]>
</script>
</html>
这是 bean 代码:
package de.example.beans;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.validator.ValidatorException;
import org.apache.log4j.Logger;
@ViewScoped
@ManagedBean
public class ResetBean implements Serializable
{
private static final long serialVersionUID = 7282752623428425109L;
private static final Logger log = Logger.getLogger(ResetBean.class);
protected String headline = null;
protected String message = null;
public ResetBean() {
log.error("ResetBean");
}
@PostConstruct
public void postConstruct() {
log.error("postConstruct");
}
@PreDestroy
public void preDestroy() {
log.error("preDestroy");
}
public void resetAction() {
log.error("resetAction");
headline = null;
message = null;
}
public void submitAction() {
log.error("submitAction headline="+headline+" message="+message);
}
public void validateForm(ComponentSystemEvent event) throws ValidatorException {
log.error("validateForm");
}
public String getHeadline() {
return headline;
}
public void setHeadline(String headline) {
this.headline = headline;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
h:command 按钮和p:remoteCommand 都以相同的方式执行相同的操作。不同之处在于,h:command 按钮响应鼠标点击,而 ESC 键在使用 ESC 键时通过 javascript 触发 p:remoteCommand。
问题是,通过p:remoteCommand 的路由似乎以某种方式破坏了支持bean(bean 是@ViewScoped)。但是,@PreDestroy 注释方法永远不会被调用:使用p:remoteCommand 后页面上的下一个操作会强制从头开始创建组件!调用默认构造函数和@PostConstruct。自然,现在缺少一些重要参数,整个视图都被枪杀了。
知道发生了什么吗?为什么在这种情况下p:remoteCommmand 和h:commandButton 之间存在差异?有没有机会解决这个问题?
【问题讨论】:
-
你能发布你的 bean 代码吗?
-
抱歉,我有一段时间不在办公室。恐怕我不能发布完整的代码,因为它太大了。我将发布一段摘录,其中包含所涉及的操作方法。
-
尝试在较小的上下文中重现错误。如果你能做到,那就没问题了。发布摘录可能很难得到明确的答案。
-
完全不确定,可能与 Mojarra 有关。您应该在 PrimeFaces 论坛上通知 Cagatay 的团队。他们将帮助您解决问题。
-
谢谢。找到答案后,我会在这里发布。
标签: jsf primefaces destroy view-scope remotecommand