1

I need to visualize some data and I want to create an javascript array based on this data.

I want to inject my own name and data to a variable from java code

/*[+
           var [+ [[${data.getDataName}]] =  [[${data.getData()}]];
+]*/

However name and data appears in brackets. Is there any walk around? Maybe you can recommend better html template engine, which works better with javascript?

Right now I have hard-coded html and javascript in my code and I want to get rid of it.

4
  • why you want create javascript array with your datas there comes from java? You be able to work with these datas (from java) directly in thymeleaf. Commented Nov 21, 2014 at 7:17
  • Because I need to plot those data using flotJS. Is there any way to do this directly in java? Commented Nov 21, 2014 at 7:26
  • ok, now i'm understand. we have a similar requirement but we handle that with ajax request. Sorry EDIT: One thing what you can do, is to call the javascript method in this what you have posted. Commented Nov 21, 2014 at 7:41
  • Ok. I found some example on documentation: var user = /*[[${session.user}]]*/ null And as I assume this should parse type to null but this does not work for me (nor parsing to array[]) Commented Nov 21, 2014 at 9:16

1 Answer 1

1

Probably solved this by now, but I'll give it a shot anyway...

I'm not sure I understand the [+ syntax, but other than that:

You probably don't want to dynamically name variables, unless you're writing your own framework or something. It will be harder for someone else to read. Also, explicitly defining variable names isn't usually considered 'hard-coding'. However, if you do want to write your own mini-framework, you could try something crazy, like the following:

var data = {};
var name = /*[[${data.getDataName()}]]*/ '';
var value = /*[[${data.getData()}]]*/ '';

data[name] = value;

However, you could also initialize a series of pairs like so: [['name1', 'value1'],['name2', 'value2'], ['name3', 'value3']]. Maybe this is what you want. Probably the easiest way to do this would be to define it in Java (as a List<Pair<String>> or something. Or you could use a List instead of a Pair, if you don't have one and don't feel like making one.

Sign up to request clarification or add additional context in comments.

Comments

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.