简体   繁体   中英

Assigning/Passing value from Java function to JS/JQuery function

Lets say I have a Java function something like

public int getNumber(){

}

which returns some value based on it's logic. And I have a JS file something like

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?

UPDATE: As suggested by abalos answer, Tap for myself has already done 3 out of 4 stages for it. 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. 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. 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.

You don't need Jersey or DWR or any other framework at all for invoking a method in 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. I'll leave this part for you to figure out from the jQuery documentation. The received data is exactly the JSONObject or JSONArray you'll return in your event handler method.

I think you have some very heavy misconceptions into what types of languages Java and jQuery/Javascript are. First off, with the exception of node.js, jQuery/Javascript are used for client-side operations. Java is used for server-side operations. 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.

Here are steps to do what you require, but note that this is not the only way.

  1. Create a web service with Jersey .
  2. Pass the value to the web service via AJAX with either JSON or XML with a request that contains the value.
  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.

Let me know if you have any questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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