3

ap: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 キーp:remoteCommandを使用して JavaScript を介してトリガーすることです。

問題は、ルート ビアがp:remoteCommand何らかの方法でバッキング Bean を破壊しているように見えることです (Bean は です@ViewScoped)。ただし、@PreDestroy注釈付きのメソッドが呼び出されることはありません。 を使用した後のページでの次のアクションではp:remoteCommand、コンポーネントが最初から作成されることを強制します! デフォルトのコンストラクターと@PostConstructが呼び出されます。当然、いくつかの重要なパラメーターが欠落しており、ビュー全体が地獄に落ちています。

何が起こっているのですか?この例でp:remoteCommmandとの違いはなぜですか? h:commandButton問題を回避する可能性はありますか?

4

1 に答える 1