Hi!

I've worked out a solution for my problem (see the attachments for
details). The problem was when I clicked on the delete button nothing
happend, the original page rerendered but the delete method never
invoked.

The problem was that when I clicked the delete button the JSF checked
the button's rendered property before invoking its action. But, as of
PageBean is request scope object, the isLoaded() method (the delete
button's rendered property is #{PageBean.loaded}) returned false,
because the bean's id attribute was null (it's a new object, because
it's request scope). You can try it, just replcae the code in the
getID() method with 'return id;'.

In my solution I use the getId() method in the isLoaded() method. In
the getId() I check is the id attribute of the bean is null. If so I
read the current value from the HTTP request paramter "form:id". No it
works fine.

My problem is it is so ugly. Getting the parameter from the HTTP
request parameters isn't fits in the JSF world - I think. I tried to
get the id from the UIInput representing the hidden input element in
the generated HTML page. But it's getSubmittedValue() return null in
the isLoaded() method. Why is that?

Is there a solution which isn't so ugly?
What do You think. Is this ugly?


Here is my code:


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().getRequestParameterMap().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>

--
 Norbert Csík

Reply via email to