2012-03-19 18 views
1

エンドユーザーが詳細を入力できるフォームを表示すると、ユーザーの入力に基づいてフィルタリングされたリストを含むテーブルが表示されます。この表には、その行を選択して詳細ビューを返すためのリンクが各行に必要です。投稿後のリンクを含むJSFテーブルの作成方法

アカウント-list.xhtml:

<?xml version="1.0" encoding="UTF-8"?> 
<ui:composition xmlns="http://www.w3.org/1999/xhtml" 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:utils="http://java.sun.com/jsf/composite/snippets" template="/WEB-INF/templates/default.xhtml"> 

    <ui:define name="title"></ui:define> 
    <ui:define name="content"> 
     <h:form id="search"> 
      <fieldset> 
       <legend>Search for account</legend> 
       <div class="account_list_section"> 
        <span class="account_section_title"><h:outputLabel for="id" value="Account ID" /></span> 
        <span class="account_list_item"> 
         <h:inputText id="id" value="#{accountsCrudBean.searchId}" styleClass="field_inputtext_account" /> 
        </span> 
       </div> 
       <div class="account_list_section"> 
        <span class="account_section_title"><h:outputLabel for="name" value="Account Name" /></span> 
        <span class="account_list_item"> 
         <h:inputText id="name" value="#{accountsCrudBean.searchName}" styleClass="field_inputtext_account" /> 
        </span> 
       </div> 
       <h:commandButton id="search" value="Display" action="#{accountsCrudBean.actionSearch}" /> 
      </fieldset> 
     </h:form> 

     <h:form id="view" rendered="#{accountsCrudBean.dataItemId.value != null}"> 
      <h:dataTable var="dataItem" binding="#{accountsCrudBean.dataTable}" value="#{accountsCrudBean.list}" id="accountListTable" 
       columnClasses="accountsCrudBeanTable_row_date, transactionsListTable_row_amount, transactionsListTable_row_balance, transactionsListTable_row_description" styleClass="transactionsListTable"> 
       <h:column headerClass="transactionsListTable_header_amount"> 
        <f:facet name="header">Id</f:facet> 
        <h:commandLink value="#{dataItem.id}" action="#{accountsCrudBean.editDataItem}" /> 
       </h:column> 
       <h:column headerClass="transactionsListTable_header_balance"> 
        <f:facet name="header">Name</f:facet>#{dataItem.name}</h:column> 
       <h:column headerClass="transactionsListTable_header_description"> 
        <f:facet name="header">Total Balance</f:facet>#{dataItem.totalBalance}</h:column> 
      </h:dataTable> 
      <h:inputHidden binding="#{accountsCrudBean.dataItemId}" /> 
     </h:form> 
    </ui:define> 
</ui:composition> 

アカウント-edit.xhtml:

<?xml version="1.0" encoding="UTF-8"?> 
<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    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:utils="http://java.sun.com/jsf/composite/snippets" 
    template="/WEB-INF/templates/default.xhtml"> 

    <ui:define name="title"></ui:define> 
    <ui:define name="content"> 
     <h:form id="edit" rendered="#{accountsCrudBean.dataItemId.value != null}"> 
      <fieldset> 
       <legend>View account 1</legend> 
       <div class="account_list_section"> 
        <span class="account_section_title"><h:outputLabel for="id" value="Account ID" /></span> 
        <span class="account_list_item"> 
         #{accountsCrudBean.dataItemId.value} 
        </span> 
       </div> 
       <div class="account_list_section"> 
        <span class="account_section_title"><h:outputLabel for="name" value="Account Name" /></span> 
        <span class="account_list_item"> 
         <h:inputText readonly="true" id="name" value="#{accountsCrudBean.dataItem.name}" styleClass="field_inputtext_account" /> 
        </span> 
       </div> 

       <div class="account_list_section"> 
        <span class="account_section_title"><h:outputLabel for="accountOwners" value="Owners" /></span> 
        <span class="account_list_item"> 
         <h:selectOneListbox readonly="true" id="accountOwners" value="#{accountsCrudBean.selectedOwner}"> 
          <f:selectItems value="#{accountsCrudBean.accountOwners}" /> 
         </h:selectOneListbox>  
        </span> 
       </div>  

       <h:inputHidden binding="#{accountsCrudBean.dataItemId}" /> 

       <h:commandButton id="edit" value="Save" action="#{accountsCrudBean.actionSearch}" /> 
      </fieldset> 
     </h:form> 
     <h:outputText value="No item selected." rendered="#{accountsCrudBean.dataItemId.value == null}" /> 
    </ui:define> 
</ui:composition> 

AccountsCrudBean.java:

@ManagedBean 
@SessionScoped 
public class AccountsCrudBean extends JsfManagedBeanBase { 

    private static final long serialVersionUID = 1L; 

    @EJB 
    AccountsManagerLocal accountsManager; 

    private Long searchId; 
    private String searchName; 

    private String selectedOwner; 

    private Account dataItem; 
    private HtmlDataTable dataTable; 
    private HtmlInputHidden dataItemId = new HtmlInputHidden(); 

    private List<Account> accountList = null; 

    public AccountsCrudBean() { 
    } 

    public Long getSearchId() { 
     return searchId; 
    } 

    public void setSearchId(Long searchId) { 
     this.searchId = searchId; 
    } 

    public String getSearchName() { 
     return searchName; 
    } 

    public void setSearchName(String searchName) { 
     this.searchName = searchName; 
    } 

    public HtmlInputHidden getDataItemId() { 
     return dataItemId; 
    } 

    public Account getDataItem() { 
     return dataItem; 
    } 

    public void setDataItem(Account dataItem) { 
     this.dataItem = dataItem; 
    } 

    public HtmlDataTable getDataTable() { 
     return dataTable; 
    } 

    public void setDataTable(HtmlDataTable dataTable) { 
     this.dataTable = dataTable; 
    } 

    public void setDataItemId(HtmlInputHidden dataItemId) { 
     this.dataItemId = dataItemId; 
    } 

    public String getSelectedOwner() { 
     return selectedOwner; 
    } 

    public void setSelectedOwner(String selectedOwner) { 
     this.selectedOwner = selectedOwner; 
    } 

    public List<Account> getList() { 
     if (accountList == null) { 
      actionLoad(); 
     } 
     return accountList; 
    } 

    private void actionLoad() { 
     if (searchId != null) { 
      Account account = accountsManager.getAccount(searchId); 
      accountList = Arrays.asList(account); 
     } else if (searchName != null) { 
      accountList = accountsManager.findAccountByName(searchName); 
     } else if (dataItemId != null && dataItemId.getValue() != null) { 
      Account account = accountsManager.getAccount(Long 
        .valueOf(dataItemId.getValue().toString())); 
      accountList = Arrays.asList(account); 
     } else 
      accountList = new ArrayList<Account>(); 
    }; 

    public void actionSearch() { 
     dataItem = (Account) dataTable.getRowData(); 
     dataItemId.setValue(dataItem.getId()); 
     actionLoad(); 
    } 

    public Map<String, User> getAccountOwners() { 
     List<User> owners = accountsManager.getUsers(dataItem); 
     LinkedHashMap<String, User> map = new LinkedHashMap<String, User>(); 
     for (User user : owners) { 
      map.put(user.getFirstName() + " " + user.getLastName(), user); 
     } 
     return map; 
    } 

    public String editDataItem() { 
     dataItem = (Account) dataTable.getRowData(); 
     dataItemId.setValue(dataItem.getId()); 
     return "accounts-edit"; 
    } 
} 

これは、JBoss 7.1にそれだ6で動作しますに失敗しました:

javax.faces.el.EvaluationException: javax.el.ELException: /s/accounts-list.xhtml at line 22 and column 94 action="#{accountsCrudBean.actionSearch}": java.lang.IllegalArgumentException: row is unavailable 
    at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:96) 
    at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:100) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:120) 
    at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:889) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:238) 
    at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1201) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:627) 
    at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34) 
    at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:171) 
    at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) 
    at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:67) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) 
    at com.demo.server.ejb.security.AuthenticationFilter.doFilter(AuthenticationFilter.java:35) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) 
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) 
    at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) 
    at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) 
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) 
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) 
    at java.lang.Thread.run(Thread.java:722) 
Caused by: javax.el.ELException: /s/accounts-list.xhtml at line 22 and column 94 action="#{accountsCrudBean.actionSearch}": java.lang.IllegalArgumentException: row is unavailable 
    at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:95) 
    at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:88) 
    ... 35 more 
Caused by: java.lang.IllegalArgumentException: row is unavailable 
    at javax.faces.model.ListDataModel.getRowData(ListDataModel.java:69) 
    at javax.faces.component.UIData.getRowData(UIData.java:462) 
    at com.demo.server.web.AccountsCrudBean.actionSearch(AccountsCrudBean.java:130) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.apache.el.parser.AstValue.invoke(AstValue.java:196) 
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) 
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43) 
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:56) 
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43) 
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:56) 
    at org.apache.myfaces.view.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:83) 
    ... 36 more 

これは、著者の使用はBeanを@RequestScopedが、私は、フィルタを@RequestScopedするBeanを変更しようとした場合は動作しません、と私はテーブルを得ることはありませんポストにhttp://balusc.blogspot.com/2006/06/using-datatables.html#AddBackingBeanActionToEveryRow

に基づいていますリンク。

ありがとうございました。

答えて

0

あなたのBeanはセッションスコープにあります。 <h:inputHidden binding="...">はまったく必要ありません。それとすべての関連コードを削除します。あなたのバッキングBeanのアクション・メソッドは次のようになります。

public void actionSearch() { 
    actionLoad(); 
} 

public String editDataItem() { 
    dataItem = (Account) dataTable.getRowData(); 
    return "accounts-edit"; 
} 

また#{accountsCrudBean.dataItem.id}によって、あなたのビューで#{accountsCrudBean.dataItemId}を交換してください。具体的な問題への無関係


、記事では、ほぼ6歳とJSF 1.xのに基づいていることに注意してください最近、JSF 2.xとEL 2.2では、選択された行を取得する他の簡単な方法があります。私のお気に入りは、私はあなたが提供する両方の方法を試してみました

public String editDataItem(Account dataItem) { 
    this.dataItem = dataItem; 
    return "accounts-edit"; 
} 
+0

<h:commandLink value="#{dataItem.id}" action="#{accountsCrudBean.editDataItem(dataItem)}" /> 

あり、そして今、私はエラーを取得しています:accountListTable: はjava.lang.IllegalStateException:重複したID「ビューを持つコンポーネントj_id1621193992_7da98672 "found – roterl

+0

物理的に異なるJSFコンポーネントを1つの同じBeanプロパティにバインドしないでください。さらに、JSF 2.xでは 'binding'は不要です。一番上の通知バーや古いブログ記事で説明したように、JSF 2.xの方法についてはhttp://balusc.blogspot.com/2010/06/benefits-and-pitfalls-of-viewscoped.htmlを参照してください。 – BalusC

+0

私は以前にそのブログを見た方がいいでしょう:) ***好きな方法でバインディング***を削除すると、Jboss 6とJBoss 7.1の両方で動作します。 ありがとうございました! – roterl

関連する問題