我有一个叫MemberSearchControler
的BackingBean。它有一个名为search
的方法。当用户调用它时,它从数据库中检索所有成员并存储在List
中,同时将所有成员的id存储在int数组中。然后返回引用member.xhtml页面的结果成员
在下文中,您可以看到MemberSearchControler
:
@ManagedBean(name="MemberSearchControler")
public class MemebrSearchControl {
private String text;
private List<Member> members;
//For Create CheckBox
private int[] membersId;
public int[] getMembersId() {
return membersId;
}
//For get Selected Member Id
private int[] selectedMembers;
public void setSelectedMember(int[] ids) {
selectedMembers=ids;
}
public List<Member> getMembers() {
return members;
}
public int getMembersCount() {
return membersCount;
}
private int membersCount;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String search() throws DaoImplNotFoundException, DataSourceException{
MemberDao dao=(MemberDao) Factory.getDao(ConfigReader.getConfig().getProperty("dao.member"));
members=dao.list(1, 10);
membersId=new int[members.size()];
for(int i=0;i<members.size();i++)
membersId[i]=members.get(i).getId();
return "member";
}
public String view(){
return "view";
}
}
在member.xhtml
我显示成员数据,我有几个复选框,返回每个成员的ID。您可以在以下内容中看到member.xhtml:
<ui:define name="center-content">
<h:form id="members">
<div class="panel panel-primary rgt">
<!-- Default panel contents -->
<div class="panel-heading">
<div style="float: left;">
<h:commandLink styleClass="btn btn-primary table-button" id="btnView" action="#{ViewMemberControler.view}">
<h:graphicImage library="img" name="view.png"
styleClass=" table-icon" />
</h:commandLink>
<h:commandLink styleClass="btn btn-primary table-button" id="btnEdit">
<h:graphicImage library="img" name="edit-member.png"
styleClass=" table-icon" />
</h:commandLink>
<h:commandLink styleClass="btn btn-primary table-button" action="add">
<h:graphicImage library="img" name="add-member.png"
styleClass=" table-icon" />
</h:commandLink>
</div>
<h4>
<h:outputText value="#{msg['protected.member.table']}" />
</h4>
</div>
<!-- Table -->
<t:selectManyCheckbox id="member" value="#{ViewMemberControler.selectedIds}" layout="spread" >
<f:selectItems value="#{MemberSearchControler.membersId}" itemLabel="" />
</t:selectManyCheckbox>
<h:dataTable styleClass="table table-condensed table-hover rgt"
value="#{MemberSearchControler.members}" var="member" id="checkall">
<h:column headerClass="cell-10">
<f:facet name="header">
<h:selectBooleanCheckbox onchange="toggle(this)"/>
</f:facet>
<t:checkbox for=":members:member" index="#{CounterControler.index}"/>
</h:column>
<h:column headerClass="cell-15">
<f:facet name="header">
<p>
<h:outputText value="#{msg['protected.member.table.family']}" />
</p>
</f:facet>
<span>#{member.lastName}</span>
</h:column>
<h:column headerClass="cell-20">
<f:facet name="header">
<p>
<h:outputText value="#{msg['protected.member.table.name']}" />
</p>
</f:facet>
<span>#{member.firstName}</span>
</h:column>
<h:column headerClass="cell-20">
<f:facet name="header">
<p>
<h:outputText
value="#{msg['protected.member.table.share.count']}" />
</p>
</f:facet>
<span>#{member.shareCount}</span>
</h:column>
<h:column headerClass="cell-35">
<f:facet name="header">
<p>
<h:outputText
value="#{msg['protected.member.table.phone.number']}" />
</p>
</f:facet>
<span>#{member.phone}</span>
</h:column>
</h:dataTable>
</div>
</h:form>
</ui:define>
</ui:composition>
正如您在上面的代码中看到的,我将用户选中的复选框发送到ViewMemberControler
而不是MemberSearchControler
。现在我有一个问题。为什么我最常使用MemberSearchControler
的@SessionScoped anntotation?如果我选择@RequestScoped将选中复选框的值发送到ViewMemberControler
将失败并再次看到member.xhtml
页面(但此时没有任何数据)。