6 Replies Latest reply on Jun 12, 2009 8:12 PM by jack yuan

    Seam javaBean using JSF binding attribute at <a4j:repeat>?

    jack yuan Newbie

      my javabean




      import java.util.List;
      import java.util.Set;
      
      import javax.faces.component.html.HtmlInputText;
      import javax.faces.component.html.HtmlSelectOneMenu;
      import javax.faces.context.FacesContext;
      
      import org.ajax4jsf.component.UIRepeat;
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Begin;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.web.RequestParameter;
      import org.jboss.seam.faces.FacesMessages;
      import org.jboss.seam.framework.EntityHome;
      
      import com.baijisoft.travel.entity.CommonCode;
      
      @Name("commonCodeHome")
      @Scope(ScopeType.CONVERSATION)
      public class commonCodeHome extends EntityHome<CommonCode>
      {
          @RequestParameter Long commonCodeId;
          private UIRepeat repeater;
           private HtmlInputText nameRef;
           private HtmlSelectOneMenu typeRef;
           @In FacesMessages facesMessages;
           
           public void change(){
                nameRef.processValidators(FacesContext.getCurrentInstance());
                nameRef.processUpdates(FacesContext.getCurrentInstance());
                typeRef.processValidators(FacesContext.getCurrentInstance());
                typeRef.processUpdates(FacesContext.getCurrentInstance());
           }
      
          @Override
          public Object getId()
          {
              if (commonCodeId == null)
              {
                  return super.getId();
              }
              else
              {
                  return commonCodeId;
              }
          }
          
          public void checkname() {
               if(this.getEntityManager().contains(this.instance))
                    this.getEntityManager().clear();
                  List exists = this.getEntityManager().createQuery("select cc from CommonCode cc where cc.name=:name and cc.type=:type")
                  .setParameter("name", this.instance.getName()).setParameter("type", this.instance.getType())
               .getResultList();
               if (exists.size()>0) {
                     facesMessages.addFromResourceBundle("myuser_username_exist", this.getInstance().getName());
                    if (this.instance.getId()==null) 
                         this.instance.setName("");
                    else {
                         this.instance.setName(this.getEntityManager().find(CommonCode.class, this.instance.getId()).getName());
                    }
               }
          }
      
          @Override @Begin
          public void create() {
              super.create();
          }
          
          public void addSub() {
               Set<CommonCode> subs = this.instance.getSubs();
               subs.add(new CommonCode());
               this.instance.setSubs(subs);
          }
          
          public void removeSub() {
               Set<CommonCode> subs = this.instance.getSubs();
               subs.remove(this.repeater.getRowData());
               this.instance.setSubs(subs);
          }
      
          
          
          
          
          
          
           public UIRepeat getRepeater() {
                return repeater;
           }
      
           public void setRepeater(UIRepeat repeater) {
                this.repeater = repeater;
           }
      
           public HtmlInputText getNameRef() {
                return nameRef;
           }
      
           public void setNameRef(HtmlInputText nameRef) {
                this.nameRef = nameRef;
           }
      
           public HtmlSelectOneMenu getTypeRef() {
                return typeRef;
           }
      
           public void setTypeRef(HtmlSelectOneMenu typeRef) {
                this.typeRef = typeRef;
           }
      
      }
      



      my xhtml




      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
          xmlns:s="http://jboss.com/products/seam/taglib"
          xmlns:ui="http://java.sun.com/jsf/facelets"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:rich="http://richfaces.org/rich"
          xmlns:a4j="http://richfaces.org/a4j"    
          template="/layout/template.xhtml">    
          
          <ui:define name="body">
           
          <h:form id="ccForm">
      
              <rich:panel>
                  <f:facet name="header">#{tagVar.comnon_code_title}</f:facet>
      
                  <s:decorate id="nameField" template="/layout/edit.xhtml">
                      <ui:define name="label">#{tagVar.common_code_name}</ui:define>
                      <a4j:region>
                      <h:inputText id="name" required="true" value="#{commonCodeHome.instance.name}" style="width:155px">
                           <f:validateLength minimum="2" maximum="50"/>
                           <a4j:support actionListener="#{myUserHome.checkname}" event="onchange" reRender="name"/>
                      </h:inputText>
                      </a4j:region>
                  </s:decorate>
                  <s:decorate id="typeField" template="/layout/edit.xhtml">
                      <ui:define name="label">#{tagVar.common_code_type}</ui:define>
                      <h:selectOneMenu value="#{commonCodeHome.instance.type}" id="type">
                           <f:selectItem itemLabel="province" itemValue="province"/>
                           <f:selectItem itemLabel="city" itemValue="city"/>
                      </h:selectOneMenu>
                  </s:decorate>
                     
                     <fieldset class="fieldset">
                          <legend class="legend">#{tagVar.common_code_sub}</legend>
                               <a4j:outputPanel id="table">
                                    <table>
                                                     <tr>
                                                        <td align="center"><h:outputText value="#{tagVar.common_code_name}" /></td>
                                                        <td align="center"><h:outputText value="#{tagVar.common_code_type}" /></td>
                                                       
                                                        <td align="center"><a4j:commandButton value="#{tagVar.new}" immediate="true" action="#{commonCodeHome.addSub}" reRender="table"
                                                             onclick="this.disabled=true" oncomplete="this.disabled=false"/></td>
                                                        <td></td>
                                                        </tr>
                                                        <a4j:repeat id="repeat" value="#{commonCodeHome.instance.subs}" var="sub" binding="#{commonCodeHome.repeater}">
                                                             <tr>
                                                            <td align="center">
                                                                <h:inputText id="subname" value="#{sub.name}" binding="#{commonCodeHome.nameRef}">
                                                                     <f:validateLength maximum="20"></f:validateLength>
                                                                     <a4j:support immediate="true" action="#{commonCodeHome.change}"
                                                                        event="onchange"/>
                                                                </h:inputText>
                                                            </td>
                                                            <td>
                                                                <h:selectOneMenu value="#{sub.type}" id="subtype" binding="#{commonCodeHome.typeRef}">
                                                                        <f:selectItem itemLabel="province" itemValue="province"/>
                                                                        <f:selectItem itemLabel="city" itemValue="city"/>
                                                                        <a4j:support immediate="true" action="#{commonCodeHome.change}"
                                                                        event="onchange"/>
                                                                   </h:selectOneMenu>
                                                            </td>
                                                           
                                                            <td>
                                                               <a4j:commandButton value="#{tagVar.delete}" immediate="true" action="#{commonCodeHome.removeSub}" reRender="table" 
                                                                    onclick="this.disabled=true" oncomplete="this.disabled=false"/>
                                                            </td>
                                                            <td>
                                                                 <rich:message for="subname"/>
                                                                 <rich:message for="subtype"/>
                                                            </td>
                                                        </tr>
                                                        </a4j:repeat>
                                                </table>
                                           </a4j:outputPanel>
                          
                     </fieldset>
      
                  
                   
                  <div style="clear:both"/>
      
              </rich:panel>
      
              <div class="actionButtons">
                  <h:commandButton id="save"
                                value="#{tagVar.submit_save}"
                               action="#{commonCodeHome.doPersist}"
                             rendered="#{!commonCodeHome.managed}"/>
                  <h:commandButton id="update"
                                value="#{tagVar.submit_update}"
                               action="#{commonCodeHome.doUpdate}"
                             rendered="#{commonCodeHome.managed}"/>
                  <s:button id="delete"
                                value="#{tagVar.submit_delete}"
                               action="#{commonCodeHome.doRemove}"
                               propagation="end"
                               immediate="true"
                            view="/my/commonCodeList.xhtml"
                             rendered="#{commonCodeHome.managed}" />
                  <s:button propagation="end"
                                     id="cancel"
                                  value="#{tagVar.submit_cancel}"
                                    view="/my/commonCodeList.xhtml"/>
              </div>
      
          </h:form>
          
                         
           </ui:define>
      </ui:composition>
      



      When I accessed that xhtml page, I got exceptions:




      Exception during request processing: 
      Caused by javax.servlet.ServletException with message: "/my/commonCode.xhtml @48,122 binding="#{commonCodeHome.repeater}": Target Unreachable, identifier 'commonCodeHome' resolved to null"
      



      What did I do wrong?


      Please help me!Thinks!




        • 1. Re: Seam javaBean using JSF binding attribute at <a4j:repeat>?
          Arbi Sookazian Master

          I usually don't use the binding attribute with the JSF or RF tags, but perhaps you're doing some dynamic manipulation of the components so you need to use binding.


          If your Seam component is null, then try adding @AutoCreate to the class...

          • 2. Re: Seam javaBean using JSF binding attribute at <a4j:repeat>?
            jack yuan Newbie

            Arbi Sookazian wrote on Jun 12, 2009 08:28:


            I usually don't use the binding attribute with the JSF or RF tags, but perhaps you're doing some dynamic manipulation of the components so you need to use binding.

            If your Seam component is null, then try adding @AutoCreate to the class...


            Thinak you very much!
            but exceptions:




            Exception during request processing: 
            Caused by javax.servlet.ServletException with message: "/my/commonCode.xhtml @48,122 binding="#{commonCodeHome.repeater}": Target Unreachable, identifier 'commonCodeHome' resolved to null" 



            bean code add @autoCreate:



            @Name("commonCodeHome")
            @Scope(ScopeType.CONVERSATION)
            @AutoCreate
            public class commonCodeHome extends EntityHome<CommonCode>
            {
            





            • 3. Re: Seam javaBean using JSF binding attribute at <a4j:repeat>?
              jack yuan Newbie

              This is One-to-many, I need edit 'one' and 'many' at the same time! And 'many' need add one or more rows.
              Please help me! or tell me other solution?
              thinks

              • 4. Re: Seam javaBean using JSF binding attribute at <a4j:repeat>?
                Arbi Sookazian Master

                I'm not sure exactly what the use-case and/or functional requirement is but you can likely use <a4j:support action="#{foo.bar}" reRender="myComponent1">.


                I'm not sure you really need to use the binding attribute...


                So if the user clicks on a row in one UI component, then you need to reRender another UI component and show n more rows or similar?


                explain the UI interaction in this page to get better feedback...

                • 5. Re: Seam javaBean using JSF binding attribute at <a4j:repeat>?
                  jack yuan Newbie

                  Thank you for your help, I am sorry, I not very good English!




                  My data table is a one-to-many, users need an interface to edit, and many do not know how many lines, it is dynamic, the need to use ajax method of increase and decrease, in the absence of seam before use, using a a4j:repeat add dynamic data line, with a4j:comandButton to Set increase and decrease, the work has been very good framework hibernate and spring and tomahawk and richfaces, state t:saveState tags with the tomahawk save.


                  The following is a seam did not use the code:


                  hbm.xml




                  <hibernate-mapping >
                   <class catalog="tf" name="MeetGoodsInfo" table="meet_goods_info">
                    <comment/>
                    <id name="id" type="int">
                     <column name="id"/>
                     <generator class="native"/>
                    </id>
                    <many-to-one class="com.baiji.bean.Unit" fetch="join" name="company" lazy="false">
                     <column name="company_id"/>
                    </many-to-one>
                    <property generated="never"  name="paicheCount" type="int">
                     <formula>(select count(*) from meet_dv_info mgi where mgi.mgi_id=id)</formula>
                    </property>
                    <property generated="never"  name="code" type="string">
                     <column length="50" name="code">
                      <comment/>
                     </column>
                    </property>
                    <many-to-one class="com.baiji.bean.Employee" fetch="join" name="employee" lazy="false">
                     <column name="employee_id"/>
                    </many-to-one>
                    <many-to-one class="com.baiji.bean.Client" fetch="join" name="client" lazy="false">
                     <column name="client_id"/>
                    </many-to-one>
                    <property generated="never"  name="sendTime" type="timestamp">
                     <column length="0" name="send_time">
                      <comment/>
                     </column>
                    </property>
                    <many-to-one class="com.baiji.bean.Employee" fetch="join" name="sendEmployee" lazy="false">
                     <column name="send_employee"/>
                    </many-to-one>
                    <property generated="never"  name="meetDatetime" type="timestamp">
                     <column length="0" name="meet_datetime">
                      <comment/>
                     </column>
                    </property>
                    <property generated="never"  name="createDate" type="timestamp">
                     <column length="0" name="create_date">
                      <comment/>
                     </column>
                    </property>
                    <one-to-one cascade="delete" name="goodsTransportBill" property-ref="meetGoodsInfo"/>
                    <property name="jhAddress" type="string">
                         <column name="jh_address" length="200"></column>
                    </property>
                    <set cascade="all" name="goodsInfos" order-by="id asc" sort="unsorted" lazy="false">
                     <key>
                      <column name="meet_goods_id">
                       <comment/>
                      </column>
                     </key>
                     <one-to-many class="com.baiji.bean.GoodsInfo"/>
                    </set>
                    <set cascade="all" name="meetDVInfos" lazy="false">
                     <key>
                      <column name="mgi_id"/>
                     </key>
                     <one-to-many class="com.baiji.bean.MeetDvInfo"/>
                    </set>
                   </class>
                  </hibernate-mapping>



                  javabean:




                  public class MeetGoodsInfoController{
                          private MeetGoodsInfo currentMeetGoodsInfo;
                       private List<MeetGoodsInfo> currentMeetGoodsInfoList;
                       private DataModel meetGoodsInfoModel;
                       private String searchCriterion;
                       
                       private List<GoodsInfo> goodsInfoList = new ArrayList<GoodsInfo>();
                       private UIRepeat repeater;
                       private HtmlInputText goodsnameRef;
                       private HtmlInputText quanlityRef;
                       private HtmlInputText weightRef;
                       private HtmlInputText dulkRef;
                       
                       
                       public MeetGoodsInfoController() {
                            currentMeetGoodsInfo = new MeetGoodsInfo();
                            PopedomUtil popeduom = new PopedomUtil();
                            boolean flage=popeduom.getPopedomVerdict("13","1");
                            if(flage!=false){
                            }
                            currentBiz = new Employee();
                       }
                  
                       public String change(){
                            goodsnameRef.processValidators(FacesContext.getCurrentInstance());
                            goodsnameRef.processUpdates(FacesContext.getCurrentInstance());
                            quanlityRef.processValidators(FacesContext.getCurrentInstance());
                            quanlityRef.processUpdates(FacesContext.getCurrentInstance());
                            this.weightRef.processValidators(FacesContext.getCurrentInstance());
                            weightRef.processUpdates(FacesContext.getCurrentInstance());
                            this.dulkRef.processValidators(FacesContext.getCurrentInstance());
                            dulkRef.processUpdates(FacesContext.getCurrentInstance());
                            return null;
                       }
                  
                       public String goodsInfoAdd() {
                            LOG.debug("goodsInfoAdd");
                  
                            GoodsInfo gi = new GoodsInfo();
                            this.goodsInfoList.add(gi);
                            return null;
                       }
                       public String goodsInfoRemove() {
                          LOG.debug("goodsInfoRemove");
                             this.goodsInfoList.remove(this.repeater.getRowData());
                          return this.change();
                       }
                  }
                  
                  ......
                  


                  page:


                  <a4j:outputPanel id="table">
                  <table>
                  <tr>
                  <td align="center"><h:outputText value="#{tagVar.goods_info_name}" /></td>
                  <td align="center"><h:outputText value="#{tagVar.goods_info_quanlity}#{tagVar.goods_info_yue}" /></td>
                  <td align="center"><h:outputText value="#{tagVar.goods_info_weight}#{tagVar.goods_info_yue}" /></td>
                  <td align="center"><h:outputText value="#{tagVar.goods_info_dulk}#{tagVar.goods_info_yue}" /></td>
                  <td align="center"><a4j:commandButton value="#{tagVar.new}" immediate="true" action="#{meetGoodsInfoController.goodsInfoAdd}" reRender="table,goodsinfo"
                  onclick="this.disabled=true" oncomplete="this.disabled=false"/></td>
                  <td></td>
                  </tr>
                  <a4j:repeat id="repeat" value="#{meetGoodsInfoController.goodsInfoList}" var="goodsInfo" binding="#{meetGoodsInfoController.repeater}">
                  <tr>
                  <td align="center">
                  <h:inputText id="goodsname" value="#{goodsInfo.goodsName}" binding="#{meetGoodsInfoController.goodsnameRef}">
                  <f:validateLength maximum="50"></f:validateLength>
                  <a4j:support immediate="true" action="#{meetGoodsInfoController.change}"
                  event="onchange"/>
                  </h:inputText>
                  </td>
                  <td>
                  <h:inputText id="quantity" value="#{goodsInfo.quantity}" size="7" binding="#{meetGoodsInfoController.quanlityRef}">
                  <f:validateLongRange minimum="1" maximum="99999999"/>
                  <a4j:support immediate="true" action="#{meetGoodsInfoController.change}"
                  event="onchange"/>
                  </h:inputText>
                  </td>
                  <td>
                  <h:inputText id="weight" value="#{goodsInfo.weight}" size="7" binding="#{meetGoodsInfoController.weightRef}">
                  <f:validateDoubleRange maximum="9999999.9999"/>
                  <a4j:support immediate="true" action="#{meetGoodsInfoController.change}"
                  event="onchange"/>
                  </h:inputText>
                  </td>
                  <td>
                  <h:inputText id="dulk" value="#{goodsInfo.dulk}" size="7" binding="#{meetGoodsInfoController.dulkRef}">
                  <f:validateDoubleRange maximum="9999999.99"/>
                  <a4j:support immediate="true" action="#{meetGoodsInfoController.change}" event="onchange"/>
                  </h:inputText>
                  </td>
                  <td>
                  <a4j:commandButton value="#{tagVar.delete}" immediate="true" action="#{meetGoodsInfoController.goodsInfoRemove}" reRender="table,goodsinfo"                                           onclick="this.disabled=true" oncomplete="this.disabled=false"/>                                                 </td>                                                 <td>                                                      </td>                                                  </tr>                                                    </a4j:repeat>
                      </table>
                  </a4j:outputPanel>
                  



                  Add meetGoodsInfoController.goodsInfoAdd through, meetGoodsInfoController.goodsInfoRemove reduce


                  Now SEAM, is do not know how to solve this problem?

                  • 6. Re: Seam javaBean using JSF binding attribute at <a4j:repeat>?
                    jack yuan Newbie

                    seam is an excellent framework, which I was attracted to many features, so in the future of the project I intend to use it, this is where the issue currently do not know how to do! Perhaps my question is what are not the problem, but I did not find the seam with the program.