4 Replies Latest reply on Mar 5, 2008 8:51 PM by bcdtech

    Postback and statefule sessionbean instances

    bcdtech

      I have the following classes:


      // package and imports removed
      
      @Local
      public interface StationLocal {
      
          public void destroy();
      
          public void setLookupString(String lookupString);
      
          public String getLookupString();
      
          public String save();
      
          public void setStation(Station station);
      
          public Station getStation();
      
          public String find();
      }


      and


      // package and imports removed
      
      @Stateful
      @Scope(ScopeType.SESSION)
      @Name("stationManager")
      @Restrict("#{identity.loggedIn}")
      public class StationBean implements StationLocal {
      
          @PersistenceContext(type = PersistenceContextType.EXTENDED)
          private EntityManager em;
          
          
          private String lookupString;
          @In(required = false)
          @Out(required = false)
          private Station station;
      
          public String find() {
      
              if (getLookupString() != null) {
      
                  try {
      
                      Query q = em.createNamedQuery("Station.findByStationName");
                      q.setParameter("stationName", getLookupString());
                      station = (Station) q.getSingleResult();
      
                  } catch (Exception e) {
                      station = new Station();
                  }
      
              } else {
                  station = new Station();
              }
      
              return "/station.xhtml";
          }
      
          public String save() {
              System.out.println(station.getId());
              System.out.println(station.getStationName());
      
              //em.merge(station);
              station = new Station();
              setLookupString("");
              return "/station.xhtml";
          }
      
          public void setLookupString(String lookupString) {
              this.lookupString = lookupString;
          }
      
          public String getLookupString() {
              return lookupString;
          }
      
          public void setStation(Station station) {
              this.station = station;
          }
      
          public Station getStation() {
              return station;
          }
      
          @Remove
          public void destroy() {
          }
      }



      then I have the following xhtml file:




      <ui:define name="body">
          <h:messages globalOnly="true" styleClass="message"/>
          <h:form id="station">
              <rich:panel>
                  <f:facet name="header">Station Edit</f:facet>
                      <ui:define name="label">Station Name</ui:define>
                      <h:inputText id="lookupString" required="true"
                                   value="#{stationManager.lookupString}"/>
                      <a:commandButton id="findStation" value="Lookup Station" action="#{stationManager.find}"/>
                      <hr/>
                      <table>                                        
                          <tr>
                              <td>
                                  <h:outputText value="Channel" />
                              </td>
                              <td>
                                  <h:inputText value="#{station.channel}" />
                              </td>
                          </tr>
                          <tr>
                              
                              <td>
                                  <h:outputText value="PI Cost" />
                              </td>
                              
                              <td>
                                  <h:inputText value="#{station.piCost}" />
                              </td>
                          </tr>
                      </table>
              </rich:panel>
          </h:form>
      </ui:define>



      What I want to do is have the user lookup a station (radio or tv) by typing 'FOX', 'ABC' or other code, clicking the lookup button (this works) and then only showing the Channel and Advertising Cost (PI Cost) fields on the view. These are the only 2 fields they should be modifying.


      With my current setup/code the values for station.id and station.stationName are null after the user clicks save, yet I have defined the station entity bean in session scope.


      What am I missing?


      Thanks
      Brent

        • 1. Re: Postback and statefule sessionbean instances
          nathandennis

          we can't see the query. we can't see the exception. why didnt you just do a select statement there?  it looks like you are redirecting to station.xhtml,, but you have the input field and the output fields in the same xhtml.??? the first time you access the page the station.blah would be null and would throw and NPE... your going to need to do something that will cause the h:outputText to not render if the station is null... something like



          rendered="#{station.name != null}"/>






          the scopes are the hardest part of seams for me to grasp.... but im pretty sure for this simple bit,,, and you are redirecting, there is no reason to store the bean in session context... conversation should do the trick

          • 2. Re: Postback and statefule sessionbean instances
            bcdtech

            It's not the query that is my issue - I get the correct results if I put in a station name and click the lookup station button. My issue comes after I get returned back to the station.xhtml after a successful query and then try to change one of the 2 fields and click save. At the time the form is submitted back, the stationManager bean only recognizes the fields that have input elements on the html form.


            It would appear I need to encapsulate all of my data (id, name, etc) into hidden input form elements.


            Thanks
            Brent

            • 3. Re: Postback and statefule sessionbean instances
              nathandennis

              While hidden inputs are an option,,, i would definitely say they are the wrong way to go from a maintenance stand point.


              i see what is happening now...


              you are referencing a bean that is not session context.


              @Name("stationManager")

              is session context. if you want to store a value in that context... reference it there.. with your getter and setter. ie... stationManager.station..

              • 4. Re: Postback and statefule sessionbean instances
                bcdtech

                Thanks - I changed the EL in my xhtml to


                #{stationManager.station.channel}



                and now I get the following error:


                javax.el.PropertyNotFoundException: /station.xhtml @28,90  value="#{stationManager.station.channel}": Target Unreachable, 'station' returned null on 'org.javassist.tmp.java.lang.Object_$$_javassist_2'"