[英]The way of accessing and setting server-side variable with client-side
我有一個使用客戶端驗證的XPage。 如果驗證失敗,它將向用戶發出alert
消息,並且不允許進程服務器端的東西。 我遇到的問題是無法為客戶端“分配”服務器端變量。 例如,考慮我有一個xp輸入字段,如下所示:
<xp:inputText
styleClass="doc_field_textinput" id="input_part_title" type="text" size="40"
disableClientSideValidation="true" >
</xp:inputText>
我使用一個按鈕來進行驗證以及驗證是否成功-保存此內容:
<xp:button id="save_part_btn" value="+Add this" style="float:right;">
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete">
<xp:this.action><![CDATA[#{javascript:
var estdoc:NotesDocument=database.getDocumentByUNID(doc_source.getDocument().getParentDocumentUNID())
var estPartdoc:NotesDocument=estdoc.getParentDatabase().createDocument()
estPartdoc.replaceItemValue('Form','Estimate_Cost_Part')
estPartdoc.replaceItemValue('Predoc',estdoc.getUniversalID())
estPartdoc.replaceItemValue('$OSN_IsSaved','1')
estPartdoc.replaceItemValue('Title', getComponent('input_part_title').getValue())
}]]>
</xp:this.action>
<xp:this.script><![CDATA[
var result = "";
var wholeResult = true;
function isStringEmpty(string2Check)
{
return string2Check == "" || string2Check[0] == " ";
}
if(isStringEmpty(document.getElementById("#{id:input_part_title}").value))
{
wholeResult = false;
result += 'The field cannot be empty!'
}
result = result.replace(/\n$/, "")
if(!wholeResult)
{
alert(result)
}
return wholeResult;
]]>
</xp:this.script>
</xp:eventHandler>
</xp:button>
不幸的是,在任何情況下, 服務器端的input_part_title
始終為null ,而document.getElementById("#{id:input_part_title}").value
效果如此之好,並且確實可以按預期的方式工作。 我希望可以向服務器端添加相同的代碼行,但是我不能,因為document
是服務器端的未知屬性。 我有什么辦法可以通過客戶端將input_part_title
分配給一個值?
最佳實踐是使用組件的value
屬性將其綁定到某些服務器端元素,例如dominoDocument數據源上的字段或viewScope
變量。 然后可以參考。
一種替代方法是使用getComponent("input_part_title").getValue()
。
將驗證器附加到組件將確保在服務器上的“過程驗證”階段進行驗證。 如果您有非文本輸入組件(例如,輸入數字),則該階段還將運行轉換器檢查並相應地中止。 然后,這將中止處理,因此按鈕的eventHandler的action
屬性中的代碼將根本不會運行。 它還將處理將錯誤消息返回到瀏覽器的方式(以編程方式,您需要查看facesContext.addMessage()
並突出顯示該組件無效( getComponent.setValid(false)
)。您。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.