0

We're developing a webpage using Spring MVC. In one of our controllers we hava a HashMap which we should send to our jsp like:

ModelAndView model = sisoController.getGenericModelAndView(request);

HashMap<String, boolean> hashMap = new HashMap<String, boolean>();
hashMap.put("name", true); 
hashMap.put("surname", false); //... and so on

model.addObject("operationFields", hashMap);

model.setViewName("createOperation.html");
return model;

Now, we need to access this hashMap in our jsp page, inside the javascript section when document.ready. Something like:

    var operationFields= ${operationFields};
    $.each(operationFields, function(key, value) {
        console.log(key + ": " + value);
    });

But this responds with the following error:

SyntaxError: missing : after property id

var operationFields= {name=true, surname=false};

How could we access the hashMap?

2
  • Your controller stored data in a model and then you suppose to use Ajax call the method and get the model then extract data from it. would you provide more code in the jsp maybe ? Commented Dec 2, 2013 at 11:48
  • The code I wrote was all the code related to the functionality. The hashMap is dinamically generated depending on the user logged (but this is not the problem). Later, on the jsp, instead of the console.log, I want to make something like $('input[name="' + key + '"]').removeClass("error");, only if its value is true. But, for this, I need to iterate through the hashMap first just when document onReady Commented Dec 2, 2013 at 11:54

1 Answer 1

1

You'll need to convert the HashMap (server side) into something accessible from the Javascript (client side). As it currently stands, the JSP is simply outputting the string value of the operationFields HashMap which is not valid Javascript syntax. It's probably easiest to convert the HashMap to a Javascript object literal.

var operationFields = {
<c:forEach var="entry" items="${operationFields}">
    '${entry.key}': '${entry.value}',
</c:forEach>
};
Sign up to request clarification or add additional context in comments.

1 Comment

OK. Trying to simplify the problem, we were using a list with those object which value was true. However, the problem was the same, as Will pointed, because javascript doesn't know what to do with this List. We've finally solved it sending an String to the javascript like this: String array = "[\'" + StringUtils.join(fields, "','") + "\']";

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.