playframework form inputs with array names - java

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]}

Related

Passing java objects from jsp page to servlet through html forms

I have a scenario in which I pass the list of objects through jsp's, its one of the attribute(name) is displayed in the drop down menu,now I want only that object should be passed again to the servlet which is selected in drop down menu while submitting the form.
What will be the best way to do it? As in forms everything is passes as string and get(through request parameter) as strings only?
I know i can pass the name and then in the servlet I can check for that name in the list of objects.But there must be some alternative solution and I am looking for that
Here are the alternatives:
Convert the values into strings and embed them in the HTML etc that gets sent to the user's browser. When the user "submits", send the strings back to the web server as request parameters where the servlet can turn the strings back into values.
Put the values into the request's Session object, and have the servlet fish them out.
Pass the values to the browsers via set-cookie, and then have the servlet extract the cookie values out of the request.
Of these, the Session approach is the most secure. If you put the values into cookies or the web form, then they can be read by the user and (maybe) changed.
You could pass a "name" I guess ... but what you are doing is reinventing the wheel of the Session object.

Displaying results on same page

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

JSP Continuos Scrolling Storing page variables in a session

I am have a currently working continuous scrolling pagination,since Http is stateless and there's no way to get back the previous data set from the HttpRequest , is it a good practice to store page variables into a user session? (like adding the id of the last content loaded so that on the next load or next request it would load the next element to it)(Assuming the page are loaded via AJAX) I have implemented it like this.
So for example, if the user search a certain name it would go to a certain action that would perform this.
paginationHelperBean = new PaginationHelper();
if(page == 0 && name != null){
paginationHelperBean.setCurrent_page(0);
} else {
paginationHelperBean.setCurrent_page(page);
}
//Set the paginationBean for display references in JSP later
paginationHelperBean.setPer_page(RESULTS_PER_PAGE);
paginationHelperBean.setTotal_count(profileService.countSearchProfiles(name, paginationHelperBean.getCurrent_page(), RESULTS_PER_PAGE));
paginationHelperBean.setNumber_of_pages();
session.put("profileSearchKey", this.name);
session.put("profileSearchPage", page);
and when the user request for another fragment or data set it would the next item from the previously loaded content it would execute action that does this.
String key = (String)session.get("profileSearchKey");
int page = (Integer)session.get("profileSearchPage")+1;
profiles = profileService.searchProfiles(key, page, RESULTS_PER_PAGE);
session.put("profileSearchPage", page);
So my question is, is it a bad practice to store page related variables in a Session for pagination(eg Search pagination with continuous scrolling?)
In my opinion doing infinite scrolling should be no different than using any ajax call to fetch additional content, you have something that triggers the fetch of additional data (the user scrolling to the bottom of the page) and when it arrives you display it. Also, you can update a JavaScript variable with the last page number fetched so that when the user scrolls to the bottom of the page again, you fetch the next page (no need to store this information in a session variable, e.g. ajax call to /nextPage.jsp?page_number=[valueFromJavaSript]).
Contrast this with storing the last page fetched in a session variable. Imagine your user navigates away from the infinite scrolling page and comes back, what do you expect should happen? Have the jsp page reset the session variable for page number when you navigate back to it, so that subsequent ajax calls fetch the right page number.
Also, if you add more than one infinite scrolling page you'll have several "current pages" stored in session.
I wanted to check how other people are doing it before I submitted my answer, I'm pretty sure that Slashdot used to have infinite scrolling, unfortunately it has a next button now. But I found an article "30+ Great Examples of Infinite Scrolling Websites" here. I used fiddler (http://www.fiddler2.com/fiddler2/) to check if they were passing the page number in the request (i.e. not storing it in session), and the ones I checked are (either page number, offset, or something similar, but the information is clearly going in the request).
is it a good practice to store page variables into a user session:No.As session attributes is not thread safe .AS it is accessible to only those with access to specific HttpSession.For better see Request and session in Servlet
The code you have written can pass through sychnonization issue.In the case when one thread is getting the value that it has set previously there will be chance that another thread has modified and set different value .So when first thread tries to get value though it may get value but it might not be right value.
The code
session.put("profileSearchKey", this.name);
session.put("profileSearchPage", page);
suppose thread A put value for profileSearchKey and profileSearchPage.When thread A tries to get value for attribute name profileSearchKey and profileSearchPage the value of this.name and page may be modified by another thread.So,on getting value there synchronization issue will arrive.

MVC Spring Model - dynamically accessing attribute list index

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.

Submitting variable length forms based on user input

I'm trying to wrap my head around this. My language is Java using PlayFramework but I think any answer using php, or ruby on rails would get me in the right direction. Basically the user can add or take away number of pallets in this form. There needs to be some javavscript calculations per tab also. Everything is fired using a input change jquery event so there is no submit buttons. My first mockup used UUID's but that seemed to get me nowhere.
What happens is each tab is reponsible for doing calculations like perimeter. My first question is how to call javascript for the specific tab. I first did it with UUID's and separate scripts for each tab. This didn't seem correct.
My second problem is submitting this data. Obviously the name parameters need to be different. So I'm scratching my head on this. Every pallet would be tagged with an id so I'm not worried about the backend as much as I'm trying to figure out how structure the front end and the controllers.
Any help would be great. Thanks.
On the javascript part, assuming all the tabs have the same structure, create a generic method that receives and id (id of the tab) as parameter, and then retrieves the proper fields form the calculation. Something like:
function doCalc(tabId){ //assumes jquery
var tab = $("#"+tabId);
var field1 = $("#"+tabId+"_field1");
var field2 = $("#"+tabId+"_field2");
//...etc to retrieve tabs
}
The method assumes that when you create a tab "tabId" and its elements, you generate the id of the elements with the formula "tabId_", which will facilitate finding them.
About the submission, using Play! you should check this section about Ajax requests. It could be as simple as doing the calculation (the method above) and at the end call a Play method via Ajax. This method would get all the parameters (id, values, etc) of a pallet (the pallet of the given tab) and save it.
For submission, I assume you are trying to figure out how to hold and pass the data in a bean. What about having a field as below:
LinkedHashMap<String, LinkedHashMap<String, Object>> field;
The inner maps would be the listing of the fields on a Pallet. The outter maps are the Pallets.

Categories