I am trying to learn JSF.
I have a simple Hello.java with annotations @ManagedBean @RequestScoped...
On index.xhtml I have:
<h:inputText id="username"
value="#{hello.name}"
required="true"
requiredMessage="A name is required.">
</h:inputText>
<h:commandButton id="submit" value="Submit" action="response">
</h:commandButton>
My question is, when I click submit button without filling the name field, I get the error message. But when I refresh the page, the error message is still there. Why does not it disappear? How can I make it disappear?
You're refreshing the form submit. So the form is submitted and validated again. A bit sane browser would have shown a warning for this like "Are you sure you want to resend post data?". Haven't you seen this?
In any case, I'm not sure what you're expecting as this is completely natural and standard HTML/webbrowser behavior on synchronous POST forms (and thus completely unrelated to JSF as it's in the context of this "problem" merely a HTML code generator), but if you want to avoid re-executing the POST request on refresh of a POST request, then the easist way is to make it an asynchronous POST request (by ajax) instead of a synchronous one. This is a matter of nesting a
<f:ajax execute="@form" render="@form" />
inside the <h:commandButton>
. The browser refresh only refreshes the last synchronous request, which would then become the initial GET request.