1

以前の質問で、メニュー バーの更新について尋ねました。BalusC から、メニュー バーを含むフォームを追加する必要があると言われました。

その質問を拡張して、タイトルのテキストを更新できるかどうか尋ねたいと思います。テンプレートが使用されており、次を使用して値を入力します

    <ui:define name="AreaTitle">
        #{viewBacking.current.firstName}  #{viewBacking.current.surName}
    </ui:define>

テンプレートには

<h:head>
<title><ui:insert name="AreaTitle">Master template</ui:insert></title>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</h:head>

ヘッダーでフォームを定義するのは奇妙に思えるので、何も定義されていません。viewBacking.current にブレーク ポイントを配置して、それがいつ使用されるかを確認できるようにします。フォームを再表示するためにリフレッシュを押しても、再びブレークポイントにヒットしません。コンテンツが異なる別のページに移動した場合にのみ、ブレークポイントに再度到達します。リフレッシュ用は

public void refreshForm() {
    RequestContext context = RequestContext.getCurrentInstance(); 
    context.update("menuForm:masterMenuBar");
    context.update("AreaTitle");
}

これは、BalusC が masterMenuBar で提供してくれた以前のソリューションを示しています。私が求めていることができないのかもしれませんが、その場合は確認をお願いします。

ありがとう、イラン

4

1 に答える 1

4

You can't update the title by JSF ajax update simply because <title> is not a JSF component. You also can't put HTML or JSF components in <title>, this is illegal HTML syntax.

Your best bet is to use JavaScript instead to update the title by assigning it to the document.title. You can use RequestContext#execute() for this.

String fullName = current.getFirstName() + " " + current.getSurName();
context.execute("document.title='" + fullName + "'");

Since this seems to be user-controlled data, I would use StringEscapeUtils#escapeJavaScript() to escape it in order to prevent potential XSS attack holes.

String fullName = current.getFirstName() + " " + current.getSurName();
context.execute("document.title='" + StringEscapeUtils.escapeJavaScript(fullName) + "'");

An alternative is to use OmniFaces <o:onloadScript>.

<o:onloadScript>document.title='#{of:escapeJS(viewBacking.current.firstName)} #{of:escapeJS(viewBacking.current.surName)}'</o:onloadScript>

This will be re-executed on every ajax request.

于 2012-05-24T13:52:19.163 に答える