简体   繁体   English

从Java函数向JS / JQuery函数分配/传递值

[英]Assigning/Passing value from Java function to JS/JQuery function

Lets say I have a Java function something like 可以说我有一个Java函数,例如

public int getNumber(){

}

which returns some value based on it's logic. 它根据逻辑返回一些值。 And I have a JS file something like 我有一个类似的JS文件

Tapestry.Validator.amountValidator = function(field, message) {

    field.addValidator(function(value) {
        if (value != null) {
          // code here
            }
        }
    });

};

Now I am asking myself is it possible in JS or JQuery to pass value from Java function to it's function(value) in JS and if so, how can it be achieved? 现在我问自己,是否可能在JS或JQuery中将值从Java函数传递给JS中的函数(值),如果可以,如何实现?

UPDATE: As suggested by abalos answer, Tap for myself has already done 3 out of 4 stages for it. 更新:正如abalos答案所建议的那样,Tap为自己完成了4个阶段中的3个。 I am providing a function that deals with server side and logic behind it. 我提供了一个处理服务器端及其背后逻辑的功能。

   @InjectComponent
    private TextField amount;
    @Inject
    private FieldValidatorSource fieldValidatorSource;

    public FieldValidator<?> getAmountValidator() 
     {
        return fieldValidatorSource.createValidators(amount, "required,max=" + getBroj());
    }

Now here validator is taken from a logic inside a function getBroj(), which is maximum number of what it takes. 现在,这里的验证器是从函数getBroj()中的逻辑中获取的,该逻辑是所需要的最大数量。 And this works like a charm on server side. 这在服务器端就像是一种魅力。 Now I was thinking that what I don't have( using my logic ) is only Client side, and I can achieve it by updating current Validation class from Tapestry that will handle with this kind of request yet known to that class. 现在我在想我所没有的(使用我的逻辑)仅是客户端,而我可以通过更新Tapestry的当前Validation类来实现,该类将处理此类已知的请求。 And to do it I would need to call a js file with a function calling something like above in the example, but I am not quite sure how to pass value from getNumber() function to the JS function above. 为此,在示例中,我需要使用一个类似于上面的函数来调用js文件,但是我不太确定如何将值从getNumber()函数传递给上面的JS函数。

You don't need Jersey or DWR or any other framework at all for invoking a method in Tapestry. 您完全不需要Jersey或DWR或任何其他框架来调用Tapestry中的方法。 You just need to ask your questions properly. 您只需要正确询问您的问题即可。

final private static String EVENT_NAME = "whateverEventNameYouWant";

@Inject
private ComponentResources resources;

@Inject
private JavaScriptSupport javaScriptSupport;

/** Method that will provide the value you want to pass to JS. */
@OnEvent(EVENT_NAME) 
public JSONObject provideValue() {
    JSONObject object = new JSONObject();
    object.put("value", /* the value you want to pass to JS */);
    // other values you may want to pass
    return object;
}

void afterRender() {
    // This creates an URL for the event you created. Requesting it will
    // invoke any event handler methods for that event name.
    Link link = resources.createEventLink(EVENT_NAME);
    javaScriptSupport.addScript("var eventUrl = '%s';", link.); // the JavaScript variable name doesn't matter. You can choose any you want
}

Then, in your JavaScript, do an AJAX request using the URL in the eventUrl variable. 然后,在JavaScript中,使用eventUrl变量中的URL进行AJAX请求。 I'll leave this part for you to figure out from the jQuery documentation. 我将把这一部分留给您从jQuery文档中了解。 The received data is exactly the JSONObject or JSONArray you'll return in your event handler method. 接收到的数据正是您将在事件处理程序方法中返回的JSONObject或JSONArray。

I think you have some very heavy misconceptions into what types of languages Java and jQuery/Javascript are. 我认为您对Java和jQuery / Java语言是什么类型有一些非常严重的误解。 First off, with the exception of node.js, jQuery/Javascript are used for client-side operations. 首先,除了node.js之外,jQuery / Javascript用于客户端操作。 Java is used for server-side operations. Java用于服务器端操作。 This means that you will need to pass a value from the server to the client. 这意味着您需要将值从服务器传递到客户端。

Now, what you are asking for looks initially like it is trying to perform validation. 现在,您想要的内容最初看起来像是尝试执行验证。 This should not be completed only on the client-side. 这不应仅在客户端上完成。 There are ways to get around client validation and it is best to leave information from the client in an "untrusted" state until it is validated on the server. 有多种方法可以避免客户端验证,最好让客户端的信息保持“不受信任”状态,直到在服务器上对其进行验证为止。

With all that said, to do what you are trying to do will require the use of some method for the client to communicate with the server. 综上所述,要做您想做的事情将需要使用某种方法让客户端与服务器进行通信。 My favorite way to do this for simple operations is through a web service. 对于简单操作,我最喜欢的方法是通过Web服务。

Here are steps to do what you require, but note that this is not the only way. 以下是执行所需操作的步骤,但是请注意,这不是唯一的方法。

  1. Create a web service with Jersey . 使用Jersey创建一个Web服务。
  2. Pass the value to the web service via AJAX with either JSON or XML with a request that contains the value. 使用包含该值的请求,通过JSON或XML通过AJAX将值传递到Web服务。
  3. Perform your validation on the server-side with the information from the service. 使用服务中的信息在服务器端执行验证。
  4. Pass a response from the rest service back to the client-side AJAX call and use it for your JS/jQuery code. 将其余服务的响应传递回客户端AJAX调用,并将其用于您的JS / jQuery代码。

Let me know if you have any questions. 如果您有任何疑问,请告诉我。

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

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