I have a question regarding how to access an index from a model attribute list dynamically.
In my code, I have some javascript which is reading a value from a model. The model has an attribute which is potentially a list.
document.getElementById("phoneNumberRPhone").value = "${model.people[index].phoneNumber.number}";
Here, you can see that I'm trying to set a javascript value to a number retrieved from a model where I can have multiple people. Index is my dynamic value. It works fine if I specifically state model.people[0] or model.people[1], but if I try to set a number to index and use index dynamically, it no longer works.
I would be very grateful for any help anyone could provide on this. I'm certain it's either just a matter of user error or improper use of syntax.
Apparently ${index} doesn't exist at all in the JSP/EL scope at the point JSP/EL has to print that piece of JS code. It would only work of you're doing for example (although this approach is highly questionable):
<c:forEach items="${model.people}" varStatus="loop">
document.getElementById("phoneNumberRPhone").value = "${model.people[loop.index].phoneNumber.number}";
</c:forEach>
Keep however in mind that JSP is merely a HTML code generator and that JavaScript is part of it. JSP and JavaScript doesn't run in sync. Rightclick page in webbrowser and do View Source to see it.
Related
I have a fairly simple question, but I'm unable to find a working answer.
I'm using javascript in my HTML file for a page that is taking input from the page and giving me back a string of a jSON object. All that I want to do is take this string and put it in a Java string.
I can do things like
target.appendJavaScript("s=getString();");
target.appendJavaScript("alert(s);")
which give me the desired information in the alert. But how do I get s into a Java string?
I have tried the following
StringValue temp = RequestCycle.get().getRequest().getQueryParameters().getParameterValue("s");
info(temp.toString());
And other variations like getRequestParameters(), but I get nulls on temp.
These actions are inside an AjaxFallbackButton's onSubmit.
Any advice on how to get a javascript var in Java?
Although I would still like to know more about sharing variables between Java and Javascript in HTML, my case was solved thanks to Wicket.
The value that I was retrieving with s=getString() was already accessible in a HiddenField, and so I simply needed to get it a wicket:id in the HTML and add it with a property model in the Java. Hopefully this might help someone else in the future.
I am new in servlets, I would like to display results in the very same page that I am on when I click a search button, then the results should be on the very same page, how can I achieve that without going to another JSP or if I am supposed to do it behind the scene moving to another without the user noticing that its another page, how do I make it seem as if its the same page with results on it. Any shed of light is highly appreciated.
You have to use javascript to receive the search results from the server. This technique is called ajax. Javascript libraries like jquery (or many others) can help you a lot with this.
Your form submits the search term to the current page, i.e. you can leave the form action attribute empty.
In your servlet check whether a search term has been submitted. If this is the case, execute your search function and write the result to the response.
You don't mention where you data is coming from, but that is fairly unimportant in this question. The data could come from a local variable, from DOM storage or have been served to you using AJAX. But here is an example of setting text data in a textarea element (there are nearly unlimited ways to format what you want, this is one example only). Dynamically changing your current page in this manner means that you do not need to navigate to another.
It works by getting a reference to the element by it's name ("data" in this case) using document.getElementById
We then set the element's content value to your data, and it is displayed in the textarea.
HTML
<input type="text"></input>
<div>
<textarea id="data"></textarea>
</div>
Javascript
var data = "Here is your data that was returned from your source";
document.getElementById("data").value = data;
On jsfiddle
I'm trying to submit a form with a few textareas like this:
<textarea name="criticism[]" rows="3" cols="5"></textarea>
The textarea needs to have an array as the name because there can be an unlimited number of them on the page, added by the user with js.
The values are passed to the controller correctly.
In the controller I do params.flash() which seems to add the values to the seession, since if I do ${flash} in the template they are printed to the screen. However, I can't access them in any way. ${flash.criticism} returns null, and ${flash.criticism[x]} will return an out of bounds error.
Am I missing anything syntax wise?
Cheers :)
The flash scope is only available to the current request and the next one. To put something in the session use session.
However flash and session are not intended to store values. They are cookie limited to 4kb. To store something use the db and/or the cache
If you want to re-render your values in the next page, just pass the string array as a 'criticism' parameter to the next render method and use it in your template with ${criticism[x]}
Using Sitebricks, I want to generate a table where one object backs each <td> in a table. The only examples I've seen have one object back an entire <tr> - so the HTML is consistent across each row. I would like to be able to wrap N entries in <tr>.
I don't want to have to have my page object in Sitebricks be aware of the layout of the page (and so have to add indices or structure the items as a List<List<Something>>).
Any ideas?
Edit: This is, of course, not limited to Sitebricks, but is a general question about separation of model from view using web templating systems.
Yep, you can add #Repeat on any tag. The implicit variables index and last are defined for you to do your own logic inside the repeat. You could, for example, add a CSS class if index % 2 == 0 to color even rows differently.
Here is a testcase showing how this works for non-table tags (the tags really don't matter):
https://github.com/dhanji/sitebricks/blob/master/sitebricks-acceptance-tests/src/main/resources/Repeat.html
Seems like you can put the #Repeat in front of anything. I don't think it cares about whether it's a row in a table or a column.
https://github.com/dhanji/sitebricks/blob/master/sitebricks-acceptance-tests/src/main/resources/Repeat.html
If you're trying to keep track of the index so you can emit special stuff every nth row, I don't know.
I have a array of data which was generated due to some Actions in my previous actions
On submitting that page, Control will be redirecting to the second page,
IN the second page, I need to populate the array data into the Tables
( I want to use Javascript to populate this, No other means)
Problems which i am facing now
1) I cant read the Java array in the Java script ? (I am not sure how to pass the Java array to Javascript function)
2) Thought of implementing the Java script objects similar to the java objects, but there will be lot of over head
Can any one help me to over come this situation
I am using the BTT framework which is similar to Struts, for Javascript I am not using any frameworks
Thanks in advance
JSON would be a format to make the java-array accessible in javascript.
https://github.com/douglascrockford/JSON-java/blob/master/JSONStringer.java
too broad to answer, since specifics are missing. here are some generic steps.
Collect your stuff in JavaScript array
Have a hidden form field
while posting to server, set the value of hidden field to your array (in string format)
Read the hidden field content in server (posted form data)
Parse them (JSON is appropriate here)
Use them while preparing next page content as JS Array