JSF Converter - Not invoked if text is read only - java

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.

Related

SelectInputText ValueChangeListener Issues in IceFaces 1.8

I am using ICEFaces 1.8 for my application. I have a few SelectInputText in a grid like this:
<ice:panelGrid columns="4">
<ice:selectInputText id="txtId"
valueChangeListener="#{employeeBean.searchIdListener}" partialSubmit="true"></ice:selectInputText>
<ice:selectInputText id="txtFirstName"
valueChangeListener="#{employeeBean.searchFirstNameListener}" partialSubmit="true"></ice:selectInputText>
<ice:selectInputText id="txtLastName"
valueChangeListener="#{employeeBean.searchLastNameListener}" partialSubmit="true"></ice:selectInputText>
<ice:selectInputText id="txtPhoneNumber"
valueChangeListener="#{employeeBean.searchPhnNbrListener}" partialSubmit="true"></ice:selectInputText>
</ice:panelGrid>
When I change the value of any one of the above SelectInputText, I can see that all the above four ValueChangeListener methods are getting executed - WHY?
Should not it be only the method getting executed whose SelectInputText have some value changes?
Please let me know about this.
It is probably happening because <ice:selectInputText> intial values are NULL and first partial or full submit will result in EMPTY string being submitted from the page.
Intialize your selectInputText with empty Strings to avoid this or you can add below in JSF 2.x,
<context-param>
<param-name>
javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL
</param-name>
<param-value>true</param-value>
</context-param>
Also to add to the Icefaces , Its partial submit is bit confusing . It is more like a Full Ajax Submit with partial Ajax/DOM rendering. There are several cases where icefaces 1.8 will execute multiple components on the page , however it avoids it in datatable column,panelSeries etc.
I think this stems from a misunderstanding of what the partialSubmit attribute is intended to do as well as a misunderstanding of the valueChangeListener.
When submitting a form by clicking a submit button lets say, all components in the form bound to a managed property value or assigned a managed bean method in valueChangeListener have their request values being submitted, processed and validated by the server. Assuming that the validation phase is successful, those submitted values are passed to the UPDATE_MODEL phase where each valueChangeListener method will be called.
Basically, this method is NOT viewed as an Application Event like a Click or Change event.
Using an Ajax submit however we can control what components in the JSF form will be submitted, and which controls will be re-rendered to display their new values after the response has been received at the client. The attribute partialSubmit however simply works in conjunction with an Ajax request to minimize the size of the ViewState and the request for performance reasons. Functionally this partialSubmit attribute alone does not have any real effect.

Rendering_response in jsf removes all the data input in h:inputText after ValidatorException

When validating fields before insertions, and adding wrong data in inputText fields,
the validatorexception causes the life-cycle to move to the rendering_response, and the input data is removed from the input text fields or keeping the old data connected to the data set.
using:
<h:inputText id="text"
value="#{cc.attrs.dataSet.data[cc.attrs.field]}"
validator="#{cc.attrs.dataSet.validate}"
>
When using a normal managed bean, the data is kept from the post submit request. Anybody have any ideas why this behavior happens?

How to re-populate form fields on a jsp page after failed server validation

I have a very simple Java MVC web application and am using a servlet to handle form validation. If the form is validated, the request is forwarded to the appropriate view. However, if the form fails validation, the request is forwarded back to the form, which then displays the appropriate error message(s).
My question is this -- what is the most efficient way to re-populate all of the form fields with the data that was originally entered in the form by the user?
I am not using an MVC framework, just simple HttpServlets as the controller with .jsp as the view.
The easiest and probably least effort is to just use
<input name="foo" type="text" value="${param.foo}"/>
This should default to "" when the user first visits the form.
A little more can be done to create a custom tag which binds to the request. However this is probably not the solution you were looking for.
Edit: You may want to use <c:out value="${param.foo}"/> to protect against XSS attack.
Pass the fields back to the jsp as part of the request object. request.setAttribute(..)
Use those attributes to set the form fields.

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.

Get user input from textbox on JSP

I need to get the value that a user has entered in a textbox on a JSP. I used JavaScript to obtain the value, but then I need to access that same variable outside the JavaScript later on in the page.
Here's what I have:
<script type="text/javascript">
var sp;
function setPolicy(){
sp = document.getElementById('policy').value;
if(sp != "")
alert("You entered: " + sp)
else
alert("Would you please enter some text?")
}
</script>
the textbox:
input type="text" id='policy' size="15" tabindex = "1" onblur="setPolicy()"
But I need to access the string entered in this "policy" textbox later on in a scriplet to call a function within my session bean. Is there any way I can pass that variable outside the JavaScript? Or is there another way to do this?
Thanks!
JavaScript execution happens at client side and Scriptlet executes at server side. You are trying to combine the two.
You should submit the form to the same page by passing a param which will have the value entered in the textbox. Your scriptlet should check if the param is present or not. First time coming on this jsp the param will not be present, it will be available only when user enter something in textbox.
May not be the best solution as I don't know the whole context.
I'm not sure you have the lifecycle of a jsp down quite yet. By the time that the javascript is running, all the scriptlets have been evaluated.
By this I mean, one the JSP is rendered, html and javascript are emitted on the response to the browser, and there is no more server to interpret the JSP.
So, you should think of your problem as how do I communicate back to the server, the result of the user action?
Probably by posting a form to an action on the server.
If you are asking how to get hold of text input value in a java session bean, it has nothing to do with your javascript code.
The session bean is server side code. To pass the input value to your session bean, you need to change server side code, a servlet, strut action, webwork action depending on which web framework you use.

Categories

Resources