Jsf viewparam lost after validation error?

Interesting case. For everyone, the following minimal code reproduces this.

Interesting case. For everyone, the following minimal code reproduces this: Facelet: #{viewParamBean. Id} Bean: @ManagedBean @RequestScoped public class ViewParamBean { private long id; private String text; public void actionMethod() { } public long getId() { return id; } public void setId(long id) { this.Id = id; } public String getText() { return text; } public void setText(String text) { this.

Text = text; } } If you call the Facelet with viewparam. Xhtml? Id=12 it will display the 12 onscreen.

If you then input something valid, e.g. Aaaaa, the id will disappear from the URL, but keeps being displayed on screen (owning to the stateful nature of ui components).However... as OP mentioned, as soon as any validator error occurs (e.g. Entering a), the id will be permanently lost. Entering valid input afterwards will not bring it back. It almost seems like a bug, but I tried both Mojarra 2.1 and Myfaces 2.1 and both have the same behavior.

Update: After some inspection, the problem seems to be in this method of `UIViewParameter' (Mojarra): public void encodeAll(FacesContext context) throws IOException { if (context == null) { throw new NullPointerException(); } // if there is a value expression, update view parameter w/ latest value after render // QUESTION is it okay that a null string value may be suppressing the view parameter value? // ANSWER: I'm not sure. SetSubmittedValue(getStringValue(context)); } And then more specifically this method: public String getStringValue(FacesContext context) { String result = null; if (hasValueExpression()) { result = getStringValueFromModel(context); } else { result = (null!

= rawValue)? RawValue : (String) getValue(); } return result; } Because hasValueExpression() is true, it will try to get the value from the model (the backing bean). But since this bean was request scoped it will not have any value for this request, since validation has just failed and thus no value has ever been set.In effect, the stateful value of UIViewParameter is overwritten by whatever the backing bean returns as a default (typically null, but it depends on your bean of course).

One workaround is to make your bean @ViewScoped, which is often a better scope anyway (I assume you use the parameter to get a user from a Service, and it's perhaps unnecessary to do that over and over again at every postback). Another alternative is to create your own version of UIViewParameter that doesn't try to get the value from the model if validation has failed (as basically all other UIInput components do).

1 For those interested; I create an issue for this at: java. Net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-1029 please vote for it if you wish it to be fixed. – Arjan Tijms Aug 2 at 21:25 I've noticed another strange behavior resulting of this issue (with Mojarra (2.1.2), but not with MyFaces (2.1.0)) : if the view parameter is set as required="true", an Ajax Request will fail in the validation Phase.

– DenisGL Aug 5 at 14:00.

You don't actually loose the view parameter. F:viewParam is stateful, so even if it's not in the URL, it's still there. Just put a break point or system.

Out in the setter bound to view param. (if you google on viewParam stateless stateful you'll find some more info).

Thanks a lot, I found an interesting article here link. However, I still have the problem: if a submit my form without any error, the setter bound to the view param is called, but If I make a mistake in my input and submit again the value is lost. – DenisGL Jul 31 at 16:17 1 The UIViewParameter is indeed stateful as I describe in that article, but due to problematic code in the encode method of that component, it overwrites it with the default value that's in the backing bean.

See my updated answer. – Arjan Tijms Jul 31 at 17:40 Okay, it's normally stateful but due to some design oversight the value is indeed lost whenever there are validation errors. Guess I learned something new!

– Mike Braun Jul 31 at 19:38.

I've the same in my Application. I switched to @ViewAccessScoped which allows way more elegant implementations.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions