8 Replies Latest reply on Oct 16, 2009 2:49 PM by Tamas Malatinszki

    Using a seam component twice in an object

    Yashwanth Gardas Newbie
      Hi,

      I'm new to Seam framework.

      I found it really interesting and some things are handled in quite an easy way!

      I'm facing the following issue:

      I have a class which has two instance variables of type "Person" like the following:

      @Name("demo)
      public class Demo{
      private Person partner;
      private Person employee;
      and their getters and setters
      }

      In the view (xhtml), I'm binding the values of the fields as #{demo.partner.firstname} and #{demo.employee.firstname}. But when I click on submit button, I get the following exception:

      javax.servlet.ServletException: /booking.xhtml @70,90 value="#{demo.partner.firstname}": Target Unreachable, 'partner' returned null on 'org.domain.entity.Demo_$$_javassist_seam_9'
           javax.faces.webapp.FacesServlet.service(FacesServlet.java:277)
           org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:83)
           org.jboss.seam.web.IdentityFilter.doFilter(IdentityFilter.java:40)
           org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:90)
           org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:64)
           org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
           org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
           org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
           org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:390)
           org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:517)
           org.jboss.seam.web.Ajax4jsfFilter.doFilter(Ajax4jsfFilter.java:56)
           org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           org.jboss.seam.web.LoggingFilter.doFilter(LoggingFilter.java:60)
           org.jboss.seam.servlet.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:69)
           org.jboss.seam.servlet.SeamFilter.doFilter(SeamFilter.java:158)
           org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


      I tried a lot of ways to get rid of the exception by using @In on the instance variables and setting the scope for Person.

      I really think this isn't that complex thing in seam, but I couldn't find out a solution to solve the issue.

      Please help me!
        • 1. Re: Using a seam component twice in an object
          Tamas Malatinszki Newbie

          Hi Yashwanth,


          Have You initialized partner and employee variables? For example:


          this.partner=new Person();
          this.employee=new Person();



          Because it seems the partner variable is null...


          Regards,


          Tamas

          • 2. Re: Using a seam component twice in an object
            Yashwanth Gardas Newbie

            Hi Tamas,


            I've tried initializing the variables in the default constructor and as well in a method having an anotation @Create.


            Now the exception doesn't occur, because the objects are created but the values(firstname) which I enter in the view are not getting saved in the respective objects!


            Please suggest me some idea!


            Regards,
            Yashwanth

            • 3. Re: Using a seam component twice in an object
              Tamas Malatinszki Newbie

              Could You show the relevant part (form and input fields) of Your xhtml code?


              Regards,


              Tamas

              • 4. Re: Using a seam component twice in an object
                Yashwanth Gardas Newbie
                The following is the form of the xhtml:

                <h:form id="tabbeddata">
                    <div class="row">
                        <div class="label">Partner's First Name</div>
                        <div>
                <h:inputText value="#{demo.partner.firstname}" class="inputText" />
                        </div>
                    </div>

                    <div class="row">
                        <div class="label">Employee's First Name</div>
                        <div>
                <h:inputText value="#{demo.employee.firstname}" class="inputText" />
                        </div>
                    </div>

                    <h:commandButton value="Submit" action="#{demoAction.toSuccess}"/>

                </h:form>
                • 5. Re: Using a seam component twice in an object
                  Yashwanth Gardas Newbie

                  Some more code related to the issue:


                  @Name(person)
                  public class Person{
                  private String firstname;
                  and its getters and setters
                  }


                  On click of Submit button on the view, I'm calling a method which returns a Success page and as well opens a pdf in new browser/tab. I'm using the bytearray of the displayed pdf to save in the filenet.


                  I don't think these things of displaying pdf or saving the byte array are a concern for the instance variables.


                  Regards,
                  Yashwanth

                  • 6. Re: Using a seam component twice in an object
                    Tamas Malatinszki Newbie

                    ...but the values(firstname) which I enter in the view are not getting saved in the respective objects!

                    One question: how did You test it? Modify the XHTML code like this only for test:


                    <h:form id="tabbeddata">
                      <div class="row">
                        <div class="label">Partner's First Name</div>
                        <div>
                          <h:inputText value="#{demo.partner.firstname}" class="inputText" />
                          <h:outputText value="#{demo.partner.firstname}"/>
                        </div>
                      </div>
                      <div class="row">
                        <div class="label">Employee's First Name</div>
                        <div>
                          <h:inputText value="#{demo.employee.firstname}" class="inputText" />
                          <h:outputText value="#{demo.employee.firstname}"/>
                        </div>
                      </div>
                      <h:commandButton value="Submit"/>
                    </h:form>



                    Can You see the entered firstname values in the h:outputText fields after pressing Submit button? If You do, it means the binding works fine (and it should work fine)...


                    Regards,


                    Tamas

                    • 7. Re: Using a seam component twice in an object
                      Yashwanth Gardas Newbie
                      It's working the way you have mentioned to do!

                      But its strange that, when I call the action on submit for my scenario to open a pdf containing that data, the pdf doesn't show the data.
                      The following is the code by which I'm trying to open a pdf and show the data I've entered in the view:

                      @Name("demo")
                      @Scope(ScopeType.CONVERSATION)
                      @AutoCreate
                      public class Demo {
                      private Person partner;
                      private Person employee;
                      and their getters and setters
                      }

                      @Name("person")
                      public class Person{
                      private String firstname;
                      and its getters and setters
                      }

                      <h:form id="tabbeddata">
                          <div class="row">
                              <div class="label">Partner's First Name</div>
                              <div>
                      <h:inputText value="#{demo.partner.firstname}" class="inputText" />
                              </div>
                          </div>
                          <div class="row">
                              <div class="label">Employee's First Name</div>
                              <div>
                      <h:inputText value="#{demo.employee.firstname}" class="inputText" />
                              </div>
                          </div>
                          <h:commandButton value="Submit" action="#{demoAction.toSuccess}"/>
                      </h:form>

                      On clicking the Submit button, I'm calling the following class:

                      @Name("demoAction")
                      @Scope(ScopeType.SESSION)
                      @AutoCreate
                      public class DemoAction{

                        /**
                         * Begin conversation.
                         */
                        @Begin
                        public String toSuccess() {
                          return "/success.xhtml";
                        }

                        public boolean generatePdf() {
                          byte[] pdfBytes = null;

                          Renderer renderer = Renderer.instance();
                          renderer.render("/generatedpdf.xhtml");
                          Context conversation = Contexts.getConversationContext();

                          DocumentStore store = (DocumentStore) conversation
                              .get("org.jboss.seam.document.documentStore");

                          ByteArrayDocumentData data = (ByteArrayDocumentData) store
                              .getDocumentData("1");

                          pdfBytes = data.getData();

                          // Code to save the bytearray for testing purpose
                          FileOutputStream fos;
                          try {
                            fos = new FileOutputStream("D:\\showdata.pdf");
                            fos.write(pdfBytes);
                            fos.close();
                          } catch (FileNotFoundException e) {
                            e.printStackTrace();
                          } catch (IOException e) {
                            e.printStackTrace();
                          }

                          endConversation();
                          return true;
                        }

                        /**
                         * End conversation.
                         */
                        @End
                        public void endConversation() {
                          return;
                        }

                      }


                      In success.xhtml, I've the script:
                           <script type="text/javascript">
                                window.open('generatedpdf.seam','temppdf');
                           </script>

                      The following is the generatedpdf.xhtml:

                      <p:document xmlns:ui="http://java.sun.com/jsf/facelets"
                           xmlns:f="http://java.sun.com/jsf/core"
                           xmlns:p="http://jboss.com/products/seam/pdf"
                           xmlns:c="http://java.sun.com/jstl/core"
                           title="Generated PDF">

                           <p:table columns="2" spacingBefore="20">
                                <f:facet name="defaultCell">
                                     <p:cell borderWidth="0" />
                                </f:facet>

                                <p:cell colspan="2">
                                     <p:font size="15">
                                <p:paragraph>Generated pdf with Information</p:paragraph>
                                     </p:font>
                                </p:cell>

                                <c:if test="#{not empty demo.partner.firstname}">
                                     <p:cell>
                                          <p:paragraph>Partner's First Name</p:paragraph>
                                     </p:cell>
                                     <p:cell>
                                          <p:paragraph>
                                          <p:text value="#{demo.partner.firstname}">
                                               </p:text>
                                          </p:paragraph>
                                     </p:cell>
                                </c:if>

                                <c:if test="#{not empty demo.employee.firstname}">
                                     <p:cell>
                                          <p:paragraph>Employee's First Name</p:paragraph>
                                     </p:cell>
                                     <p:cell>
                                          <p:paragraph>
                                          <p:text value="#{demo.partner.firstname}">
                                               </p:text>
                                          </p:paragraph>
                                     </p:cell>
                                </c:if>

                           </p:table>
                      </p:document>

                      In the generatedpdf, I cannot get the values displayed and also the byte array which I'm using it to save in the filenet (As of now, I'm trying to save it in file system as D:\\showdata.pdf as shown in the code) doesn't contain the values.

                      Please let me know, if you need any more details.

                      Regards,
                      Yashwanth




                      • 8. Re: Using a seam component twice in an object
                        Tamas Malatinszki Newbie

                        Change ScopeType of Demo class from CONVERSATION to SESSION, and it will work fine.


                        Regards,


                        Tamas