Play! Need of Flash for input field - java

I am using Play 1.2.5. In the below statement (got it in Docs):
Name: <input type="text" name="name" value="${flash.name}" />
what is the use/need of value input text parameter and what does ${flash.name} means for the value field?
Please let me know about this.
Regards,

value will be the "default" value or the value to be filled on the page load (for a good example when the users inputs invalid data and you want to show or leave what he has already placed in that field, or to have text fields already filled for the user). More info here: site
the flash.name flash is a "container" of key\value pairs that are received on the request by cookie and set by you previously, it works like session but only last for one request. You can find more info here: site

Related

Spring errors tag, show additional errors

I have a password field defined in JSP
<form:password path="userprofile.password" id="password" />
form taglib refers to Spring form taglib (http://www.springframework.org/tags/form)
The JSP form contains other fields as well e.g. .
<form:input path="userform.name" />
Since this is a password, even if user has provided valid password, it disappears on page reload.
Consider this scenario:
User has provided a valid password, but there is an error in "Name" field. on submit, page reloads and the error for "Name" is displayed, and password field is emptied out.
I want to show a message to user that the entered password is emptied due to security reasons and the user needs to enter that as well.
However, if the error is in password field itself, this additional message should not be shown.
Please suggest a solution.
PS:
I tried the solutions at Spring Forms - How to Check for Error on Specific Path
They work for all fields apart from password, where I want.
I could not find a way to do the same in JSP. However, at spring controller (when the form was submitted) I used the following code to make it work:
if (!errors.hasFieldErrors("password")){
errors.rejectValue("password", "My Error Message Key....");
}
PS:
errors is the instance of org.springframework.validation.Errors which we get from Spring framework.

Sending text-field value to server without form tag

I need to post a text-field value to server but i have not placed the text-field in side the form tag.Here is the details of my use-case
i have an anchor tag like
LOGIN
This anchor tag is not inside any form tag and i need to send one extra value to the server and don't want that value to append as query string.
I have created a hidden filed and have provided the required value to that hidden field, but when i click on the Login link and its getting to my Controller class this hidden field value is not available.
Is there any way to send that value to server side class as a request parameter?
You can use ajax to do that, i suggest to use Jquery
$.post('loginhandle', {username:$('#username').val(), password: $('#password').val()} function(){});
By using Javascript get value from hidden fields like this
<script>
var name= document.getElementById("login").value
document.getElementById("topage").innerHTML='LOGIN'
</script>
<input type="hidden" name="name" value="ashraf" id='login'>
<div id='topage'>
LOGIN
</div>
You're doing a get rather than a post. You could append to he querystring as this works with get.
Get the hidden field value using javascript before the form is submitted. Use
document.getElementById("hiddenID").value; Append the value obtained in the URL before the form is submitted. The value should be there in the server.
Regards,
Ajai G

JSF Converter - Not invoked if text is read only

Context : One of the application has a xhtml form where a text area would be rendered as disabled field.
Problem : And when user submits the form, the converter associated with that field doesn't get invoked, but works fine when the text area is not disabled.
Is this how JSF request/response life cycle works or, am I missing something ?
Pseudo Code:
<tag:field id="xyz" label="abc" value="#{something.something}"
disaply="mutable" required="false" styleClass="Text_Area"
disabled="#{somethingelse.something}" rows = "4" cols="50"
converter="freeTextConverter">
</tag:field>
As per HTML specification, the values of disabled input fields are not sent along with the form submit. So JSF simply got null as value and there's nothing to convert. You perhaps want to use readonly instead. This way the value is not editable, but it will be sent along with the form submit. You only need to take into account that the client can still tamper the request and edit the value before it get sent. You'd perhaps want to store the value in a view scoped bean instead and keep the field disabled.

Strange behavior with the method getUploadedBlobs

I've a problem with the methode blobstoreService.getUploadedBlobs(). I've a JSP page in wich one I set an uploader like this :
<formname='form' action='<%= blobstoreService.createUploadUrl("/Edit_Engine") %>' method='POST' enctype='multipart/form-data' >
<input label='...' multiple='false' name='myFile' />
//...and multiple input for text
</form>
and I retrieve this code with my servlet :
java.util.Map<String,BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
BlobKey blobK = blobs.get("myFiles[]"); //I don't know why I need to add the characters 's[]' at the end...
But the behavior is strange. The first time I upload an image, everything works. However, the second time, I send my form without uploading somehting (only text data), and then my java code finds a BlobKey. But this BlobKey seems to be the previous sended data, or a corrupted data.
I mean that not normal, because when I deploy this version on my localhost, if the form uploads no file the method getUploadedBlobs returns an empty HashMap. However, when I deploy on google servers, if the form uploads no file, the method getUploadedBlobs seems to return a HashMap with wrong data.
Could you help me? Or tell me if this behaviro is normal...
Many thanks,
bat
If you're getting a valid BlobKey, then myFiles[] is most likely the name given to the file input field in the form. Is that the case? That seems like an odd name for an input field. Are you using a template library to help generate HTML from the JSP?

Using request.setAttribute in a JSP page

Is it possible to use request.setAttribute on a JSP page and then on HTML Submit get the same request attribute in the Servlet?
No. Unfortunately the Request object is only available until the page finishes loading - once it's complete, you'll lose all values in it unless they've been stored somewhere.
If you want to persist attributes through requests you need to either:
Have a hidden input in your form, such as <input type="hidden" name="myhiddenvalue" value="<%= request.getParameter("value") %>" />. This will then be available in the servlet as a request parameter.
Put it in the session (see request.getSession() - in a JSP this is available as simply session)
I recommend using the Session as it's easier to manage.
The reply by Phil Sacre was correct however the session shouldn't be used just for the hell of it. You should only use this for values which really need to live for the lifetime of the session, such as a user login. It's common to see people overuse the session and run into more issues, especially when dealing with a collection or when users return to a page they previously visited only to find they have values still remaining from a previous visit. A smart program minimizes the scope of variables as much as possible, a bad one uses session too much.
You can do it using pageContext attributes, though:
In the JSP:
<form action="Enter.do">
<button type="SUBMIT" id="btnSubmit" name="btnSubmit">SUBMIT</button>
</form>
<% String s="opportunity";
pageContext.setAttribute("opp", s, PageContext.APPLICATION_SCOPE); %>
In the Servlet (linked to the "Enter.do" url-pattern):
String s=(String) request.getServletContext().getAttribute("opp");
There are other scopes besides APPLICATION_SCOPE like SESSION_SCOPE. APPLICATION_SCOPE is used for ServletContext attributes.
If you want your requests to persists try this:
example: on your JSP or servlet page
request.getSession().setAttribute("SUBFAMILY", subFam);
and on any receiving page use the below lines to retrieve your session and data:
SubFamily subFam = (SubFamily)request.getSession().getAttribute("SUBFAMILY");
Try
request.getSession().setAttribute("SUBFAMILY", subFam);
request.getSession().getAttribute("SUBFAMILY");
Correct me if wrong...I think request does persist between consecutive pages..
Think you traverse from page 1--> page 2-->page 3.
You have some value set in the request object using setAttribute from page 1, which you retrieve in page 2 using getAttribute,then if you try setting something again in same request object to retrieve it in page 3 then it fails giving you null value as "the request that created the JSP, and the request that gets generated when the JSP is submitted are completely different requests and any attributes placed on the first one will not be available on the second".
I mean something like this in page 2 fails:
Where as the same thing has worked in case of page 1 like:
So I think I would need to proceed with either of the two options suggested by Phill.
i think phil is right request option is available till the page load. so if we want to sent value to another page we want to set the in the hidden tag or in side the session if you just need the value only on another page and not more than that then hidden tags are best option if you need that value on more than one page at that time session is the better option than hidden tags.

Categories

Resources