1

Here is my markup, I am using jtsl core tag

<c:forEach var="attr" items="${attributes}">
    <c:when test='${attr.controlType == "textField"}'>
        <script>createTextField("${attr}");</script>
        </c:when>
</c:forEach>

So the "attributes" is a list of objects which resides in the model.

I want to call the createTextField function and I want access to the "attr" in that function.

Here is my function, but I can't get access to the object, says it is undefined.

function createTextField(object) {
    document.write(object.name);        
}

Any ideas? would be appreciated.

1 Answer 1

4

This is not going to work. Java and JavaScript doesn't run in the same environment. You're basically passing attr.toString() to the JavaScript function which look by default like com.example.ClassName@hashcode. The JavaScript String object doesn't have a name property.

There are basically two ways to get it to work:

  1. You need to convert the Java object as represented by #{attr} to a string which conforms the JavaScript object notation (also known as JSON). E.g.

    <script>createTextField(${attr.asJson});</script>
    

    with something like (with little help of Gson):

    public String getAsJson() {
        return new Gson().toJson(this);
    }
    

    You can of course also manually build it using StringBuilder or something. JSON format isn't that hard.

  2. Pass only the property/properties of interest as long as they can be represented as String. E.g.

    <script>createTextField("${attr.name}");</script>
    

    with

    function createTextField(name) {
        document.write(name);        
    }
    
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, in this case option 2 will not work because I have a lot of fields in the object that I need in the function so, I have to use option one, but I am confused, I know what JSON is but dont understand what you meant by number 1. Can you please explain in a bit more detail?
What part exactly don't you understand? Option 1 just tells you that you should convert the Java object to JSON so that you can easily print it as a valid JS function argument.
how to convert that object to a JSON, and then how to extract the values from that JSON within that JS function
Uh, just use Gson or any arbirary Java based JSON parser API to convert a Java object to JSON string, or create a String in JSON format yourself with StringBuilder. The format should ultimately end up something like {"name":"somevalue", "otherproperty":"othervalue"} etc. Your JS createTextField() function can be kept untouched as it already retrieves a valid JS object when using JSON. You said that you know what JSON is, but after all you apparently don't understand JSON at all. What exactly is your problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.