简体   繁体   English

用客户端访问和设置服务器端变量的方式

[英]The way of accessing and setting server-side variable with client-side

I have an XPage which uses client-side validation. 我有一个使用客户端验证的XPage。 If validation fails it gives the user an alert message and won't permit proccess server-side stuff. 如果验证失败,它将向用户发出alert消息,并且不允许进程服务器端的东西。 The problem I've encountered is being unable to "assign" server-side variables with client-side. 我遇到的问题是无法为客户端“分配”服务器端变量。 For example, consider I have an xp input field like this: 例如,考虑我有一个xp输入字段,如下所示:

<xp:inputText 
  styleClass="doc_field_textinput" id="input_part_title" type="text" size="40" 
  disableClientSideValidation="true" >
</xp:inputText>

And I use a button in order to validate and if validation succeeds - save this: 我使用一个按钮来进行验证以及验证是否成功-保存此内容:

<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>

Unfortunatly, the input_part_title on the server-side is always null , under any circumstances, whereas document.getElementById("#{id:input_part_title}").value works so well, and really works the way it's expected. 不幸的是,在任何情况下, 服务器端input_part_title始终为null ,而document.getElementById("#{id:input_part_title}").value效果如此之好,并且确实可以按预期的方式工作。 I wish I could add the same line of code to my Server-Side, but I can't because document is an unknown property on the server side. 我希望可以向服务器端添加相同的代码行,但是我不能,因为document是服务器端的未知属性。 Is there any way I can somehow assign input_part_title to a value from client-side? 我有什么办法可以通过客户端将input_part_title分配给一个值?

Best practice is to use the value property of the component to bind it to some server-side element, eg a field on a dominoDocument datasource or a viewScope variable. 最佳实践是使用组件的value属性将其绑定到某些服务器端元素,例如dominoDocument数据源上的字段或viewScope变量。 You can then reference. 然后可以参考。

An alternative is to use getComponent("input_part_title").getValue() . 一种替代方法是使用getComponent("input_part_title").getValue()

Attaching a validator to the component will ensure validation occurs during the Process Validations phase on the server. 将验证器附加到组件将确保在服务器上的“过程验证”阶段进行验证。 If you have non-text input components (eg to take a number), that phase also runs converter checks and aborts accordingly. 如果您有非文本输入组件(例如,输入数字),则该阶段还将运行转换器检查并相应地中止。 That will then abort processing, so your code in the button's eventHandler's action property won't run at all. 然后,这将中止处理,因此按钮的eventHandler的action属性中的代码将根本不会运行。 It will also handle returning the error message to the browser (programmatically you'd need to look at facesContext.addMessage() as well as highlighting the component as being invalid ( getComponent.setValid(false) ). The validator handles all of that for you. 它还将处理将错误消息返回到浏览器的方式(以编程方式,您需要查看facesContext.addMessage()并突出显示该组件无效( getComponent.setValid(false) )。您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM