クライアントサイドJavascriptで処理した値をサーバーサイドJavascriptで受け取る方法

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">

	<!-- 「style="display:none;"」で非表示フィールドを作成 (デバッグ時は「display:block;」で表示させると便利) -->
	<xp:inputText id="hiddenCustomVal" style="display:block;"
		value="#{requestScope.customVal}">
	</xp:inputText>
	<xp:button value="CSJS処理後にサーバーへPOST処理" id="button1">
		<xp:eventHandler event="onclick" submit="true"
			refreshMode="complete" disableValidators="true">
			<xp:this.script><![CDATA[var nw = new Date();
console.log(nw);
document.getElementById("#{id:hiddenCustomVal}").value = nw;
]]></xp:this.script>
			<xp:this.action><![CDATA[#{javascript:print(requestScope.customVal);}]]></xp:this.action>
		</xp:eventHandler>
	</xp:button>
</xp:view>





ClientSide Javascript(CSJS)で処理した値をServerSide Javascript(SSJS)で受け取るためのサンプルになります。

このサンプルではCSJSで取得した日付の値をrequestScopeでバインドさせた格納用のInputフィールドを介してSSJSへ値を渡しています。

CSJSで値をセットする際は

document.getElementById("#{id:hiddenCustomVal}").value

というようにInputフィールドのID属性を利用。

SSJSではバインドされたrequestScope.customVal を利用していることに注意してください。


XPages
kazut
July 17, 2018 at 4:16 PM
Rating
0





No comments yetLogin first to comment...