You are right. I have just come to understand that navigating to the same
view name actually recreates the view (hence resetting the bean), whereas a
null outcome stays on the same view (expeced).
With that in mind, I can fix my code that way:
public class PersonPage {
private Map getRequestMap() {
return
FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
}
public String console() {
Person person = (Person) getRequestMap().get("person");
System.out.println(person.getLastname()+" "+person.getFirstname());
return null;
}
public String reset() {
getRequestMap().remove("person");
FacesContext.getCurrentInstance().renderResponse();
return "reset"; // navigating to the same view
}
}
<managed-bean>
<managed-bean-name>personPage</managed-bean-name>
<managed-bean-class>reset.PersonPage</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/reset/person.jsp</from-view-id>
<navigation-case>
<from-outcome>reset</from-outcome> <!-- navigating to the same view
-->
<to-view-id>/reset/person.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<h:form>
Lastname : <h:inputText id="lastname"
value="#{person.lastname}" required="true"/><p/>
Firstname : <h:inputText id="firstname"
value="#{person.firstname}"/><p/>
<h:commandButton value="Console"
action="#{personPage.console}"/>
<h:commandButton value="Reset" immediate="true"
action="#{personPage.reset}"/>
</h:form>
-----Message d'origine-----
DeÂ: CsÃk Norbert [mailto:[EMAIL PROTECTED]
EnvoyÃÂ: mercredi, 27. avril 2005 11:49
ÃÂ: MyFaces Discussion
ObjetÂ: Re: resetting a form
Check out my clear button. In faces-config I have a navigation rule
for the "clear" outcome: from page.jspx to page.jspx. It works for me.
public class PageBean {
private String id;
private String name;
public PageBean() {
}
public String save() {
FacesContext.getCurrentInstance().addMessage(null, new
FacesMessage("Saved."));
id = "15";
return "success";
}
public String delete() {
FacesContext.getCurrentInstance().addMessage(null, new
FacesMessage("Deleted."));
return clear();
}
public String clear() {
id = "";
return "clear";
}
public String editDetail() {
FacesContext.getCurrentInstance().addMessage(null, new
FacesMessage("Detail."));
return "success";
}
public boolean isLoaded() {
return getId() != null && !getId().equals("");
}
public String getId() {
return (id == null)
?
(String)FacesContext.getCurrentInstance().getExternalContext().getRequestPar
ameterMap().get("form:id")
: id
;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
And the JSF page:
<?xml version='1.0' encoding='ISO-8859-2'?>
<jsp:root xmlns="http://www.w3.org/1999/xhtml"
xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<f:view>
<html>
<jsp:output omit-xml-declaration="false" doctype-root-element="html"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/>
<jsp:directive.page contentType="text/html;charset=ISO-8859-2"/>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-2"/>
<title>
Page
</title>
</head>
<body>
<h:form id="form">
<h:panelGrid columns="1">
<h:messages layout="table"/>
<h:panelGroup>
<h:commandButton value="New" action="#{PageBean.clear}"
immediate="true"/>
<h:commandButton value="Save" action="#{PageBean.save}"/>
<h:commandButton value="Delete" action="#{PageBean.delete}"
immediate="true"
rendered="#{PageBean.loaded}"/>
</h:panelGroup>
<h:inputHidden id="id" value="#{PageBean.id}"/>
<h:panelGrid columns="2">
<h:outputLabel style="color:red"
value="NÃv"/>
<h:inputText value="#{PageBean.name}"
required="true"/>
<h:outputLabel value="ID"/>
<h:outputText value="#{PageBean.id}"/>
<h:outputLabel value="TÃpus"/>
<h:commandButton
action="#{PageBean.editDetail}" value="Szerkeszt"
disabled="#{!PageBean.loaded}"/>
</h:panelGrid>
</h:panelGrid>
</h:form>
</body>
</html>
</f:view>
</jsp:root>
On 4/26/05, Vincent SEVEL <[EMAIL PROTECTED]> wrote:
>
>
>
> Hi, I am trying to figure out the best/simplest way to reset a form whose
> fields are associated with a backing bean. I have found one incomplete
> solution and would like to hear from other people.
>
>
>
> I have got a person.jsp, which contains:
>
> Ã Input text bound to #{person.lastname}, required=true
>
> Ã Input text bound to #{person.firstname}
>
> Ã Button bound to #{personPage.console} that displays the content of
> the form in the console
>
> Ã Button bound to #{personPage.reset} that blanks out all the fields
> from the form
>
>
>
> 1) Initially I just removed the "person" object from the request map and
> returned null from the reset action. This caused the form to be reset, but
> also an validation error message to be displayed on the next page about
the
> first field not being filled (ie: it is required).
>
>
>
> 2) Then, I set immediate='true' on the reset button, and rendered the
> response directly from the reset action (ie:
> FacesContext.getCurrentInstance().renderResponse()). Although this skipped
> the validation phase, it did not repopulate the components from the value
> binding expressions (ie: it does not blank out the form).
>
>
>
> 3) The only and last solution I can see at this time would be to
> findComponent() the lastname & firstname fields, blank them out manually
and
> renderResponse() immediately. But this extremely tedious and error prone.
>
>
>
> In short, I would like to deactivate validation and conversion for some
> special actions, but still feed from the backing bean during the render
> phase. Any ideas?
>
>
>
> Thanks, source follows.
>
> Vincent
>
>
>
> person.jsp
>
> <f:view>
>
> <h:messages styleClass="error" layout="table"
showDetail="true"
> showSummary="false" tooltip="true"/>
>
> <h:form>
>
> Lastname: <h:inputText id="lastname"
> value="#{person.lastname}" required="true"/><p/>
>
> Firstname: <h:inputText id="firstname"
> value="#{person.firstname}"/><p/>
>
> <h:commandButton value="Console"
> action="#{personPage.console}"/>
>
> <h:commandButton value="Reset" immediate="true"
> action="#{personPage.reset}"/>
>
> </h:form>
>
> </f:view>
>
>
>
> public class Person {
>
> private String lastname;
>
> private String firstname;
>
> â
>
>
>
> public class PersonPage {
>
>
>
> private Map getRequestMap() {
>
> return
> FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
>
> }
>
>
>
> public String console() {
>
> Person person = (Person) getRequestMap().get("person");
>
> System.out.println(person.getLastname()+" "+person.getFirstname());
>
> return null;
>
> }
>
>
>
>
>
> public String reset() {
>
> getRequestMap().remove("person");
>
> FacesContext.getCurrentInstance().renderResponse(); // will skip the
> validation and conversion phases, but will not refresh from the backing
bean
>
> return null;
>
> }
>
> }
--
CsÃk Norbert http://norbert.web.elte.hu/
ProgramtervezÅ matematikus
Trilobita Informatikai Rt. - rendszertervezÅ fejlesztÅmÃrnÃk
___ keep sm:)ing _________________________ooo__C( O O )L__ooo__
http://www.aion.hu/ - A csik.NET otthona
http://www.spreadfirefox.com/ - Rediscover the web
LÃgy pontos: MÃrj mikro-millimÃterben! JelÃlj krÃtÃval! VÃgj baltÃval!