2012-07-24 99 views
1

我創建了一個web應用程序。在這裏我使用prime-face選項卡和ajax。 當我點擊菜單欄上的菜單項時,它會打開一個新選項卡。但激活第一個打開的選項卡。我想激活上次打開的選項卡。 我的代碼在這裏:如何通過點擊菜單欄上的菜單項來激活上次打開的選項卡?

的菜單欄和輸出選項卡

<h:form id="menu">     
      <p:menubar autoDisplay="true"> 
       <p:submenu id="student" label="Student">  
        <p:menuitem id="studentAdmission" value="Student Admission" action="#{ajaxBean.editAction}" ajax="true" update=":outputForm"> 
         <f:setPropertyActionListener target="#{ajaxBean.action}" value="StudentAdmission" /> 
        </p:menuitem> 
        <p:menuitem id="studentList" value="Student Profile" action="#{ajaxBean.editAction}" ajax="true" update=":outputForm"> 
         <f:setPropertyActionListener target="#{ajaxBean.action}" value="StudentProfile" /> 
        </p:menuitem>       
       </p:submenu> 
       <p:submenu id="registration" label="Registration"> 
        <p:menuitem id="registrationAdd" value="Subject Registration" action="#{ajaxBean.editAction}" ajax="true" update=":outputForm"> 
         <f:setPropertyActionListener target="#{ajaxBean.action}" value="SubjectRegistration"/>         
        </p:menuitem> 
        <p:menuitem id="registrationList" value="List of Registration" action="#{ajaxBean.editAction}" ajax="true" update=":outputForm"> 
         <f:setPropertyActionListener target="#{ajaxBean.action}" value="RegistrationList" /> 
        </p:menuitem>       
       </p:submenu> 
      </p:menubar> 
     </h:form>  
     <h:form id="outputForm"> 
      <p:tabView id="outputTab"> 
       <p:ajax event="tabClose" listener="#{ajaxBean.closeTab}"/> 
       <c:forEach items="#{ajaxBean.chcekItem}" var="item" varStatus="loop"> 
        <p:tab id="#{item}" title="#{item}" closable="true">        
         <ui:include src="#{bundle[item]}"/> 

        </p:tab> 
       </c:forEach> 
      </p:tabView> 
     </h:form> 

ajaxBean.java

package com.ajax; 

import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 
import org.primefaces.event.TabCloseEvent; 

/** 
* 
* @author DELL 
*/ 
@ManagedBean(name = "ajaxBean") 
@SessionScoped 
public class AjaxBean implements Serializable { 

public AjaxBean() { 
} 
private boolean input; 
private List<String> chcekItem = new ArrayList<String>(); 

public void setAction(String action) { 
    input = true; 
    if (getChcekItem().isEmpty()) { 
     getChcekItem().add(action); 
     System.out.println("The item " + action + " is Successfully Added to array"); 
    } else { 

     for (String abce : getChcekItem()) { 
      if (abce.equals(action)) { 
       System.out.println(abce + "=" + action); 
       input = false; 
       //We can jump out of the loop here since we already found a matching value 
       break; 
      } 
     } 

     if (input) { 
      getChcekItem().add(action); 
      System.out.println("The item " + action + " is Successfully Added to array"); 
     } else { 
      System.out.println("The item " + action + " is Exist"); 
     } 
    } 


} 
private String close; 

public void closeTab(TabCloseEvent closeAction) { 
    close = closeAction.getTab().getId(); 
    for (String abce : getChcekItem()) { 
     if (abce.equals(close)) { 
      System.out.println(close + " is remove from Array"); 
      getChcekItem().remove(abce); 
      //We can jump out of the loop here since we already found a matching value 
      break; 
     } 
    } 
} 

public String editAction() { 
    return null; 
} 

/** 
* @return the arrJavaTechnologies 
*/ 
/** 
* @return the chcekItem 
*/ 
public List<String> getChcekItem() { 
    return chcekItem; 
} 

/** 
* @param chcekItem the chcekItem to set 
*/ 
public void setChcekItem(ArrayList<String> chcekItem) { 
    this.chcekItem = chcekItem; 
} 

/** 
* @return the input 
*/ 
public boolean isInput() { 
    return input; 
} 

/** 
* @param input the input to set 
*/ 
public void setInput(boolean input) { 
    this.input = input; 
} 
/** 
* @return the menuaction 
*/ 

}

item.java

package com.ajax; 


public class Item { 

public Item() { 
} 
private String value; 

public Item(String value) { 
    this.value = value; 
} 

public void setValue(String value) { 
    this.value = value; 
} 

public String getValue() { 
    return value; 
} 
} 

MenuManagedBean.java

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
    */ 
package com.ajax; 

import java.util.ArrayList; 
import java.util.Iterator; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 

/** 
* 
* @author DELL 
*/ 
@ManagedBean(name = "menuManagedBean") 
@SessionScoped 
public class MenuManagedBean { 

/** Creates a new instance of MenuManagedBean */ 
public MenuManagedBean() { 
} 

private String menuItems; 
private ArrayList<String> menuId=new ArrayList<String>(); 

public void tabGenerate(String event){ 
    this.menuItems=event; 
    getMenuId().add(event); 

    Iterator iterator=getMenuId().iterator(); 
    while(iterator.hasNext()){ 
     System.out.println("Items : " +iterator.next()); 
    } 
} 

/** 
* @return the menuId 
*/ 
public ArrayList<String> getMenuId() { 
    return menuId; 
} 

/** 
* @param menuId the menuId to set 
*/ 
public void setMenuId(ArrayList<String> menuId) { 
    this.menuId = menuId; 
} 
} 

請幫我在這話題。

回答

2

您添加JSF頁面流命令:

ajaxBean.java的
<p:tabView id="outputTab" activeIndex="#{ajaxBean.count}"> 

改變流:

@ManagedBean(name = "ajaxBean") 
    @SessionScoped 
    public class AjaxBean implements Serializable { 

    public AjaxBean() { 
    } 
    private boolean input; 
    private List<String> chcekItem = new ArrayList<String>(); 
    private int count; 

    public void setAction(String action) { 
    input = true; 
    if (getChcekItem().isEmpty()) { 
    count++; 
    getChcekItem().add(action); 
    System.out.println("The item " + action + " is Successfully Added to array"); 
    } else { 

    for (String abce : getChcekItem()) { 
     if (abce.equals(action)) { 
      System.out.println(abce + "=" + action); 
      input = false; 
      //We can jump out of the loop here since we already found a matching value 
      break; 
     } 
    } 

    if (input) { 
     count++; 
     getChcekItem().add(action); 
     System.out.println("The item " + action + " is Successfully Added to array"); 
    } else { 
     System.out.println("The item " + action + " is Exist"); 
    } 
    } 


    } 
    private String close; 

    public void closeTab(TabCloseEvent closeAction) { 
    close = closeAction.getTab().getId(); 
    for (String abce : getChcekItem()) { 
    if (abce.equals(close)) { 
     count--; 
     System.out.println(close + " is remove from Array"); 
     getChcekItem().remove(abce); 

     //We can jump out of the loop here since we already found a matching value 
     break; 
     } 
     } 
     } 

    public String editAction() { 
      return null; 
        } 


     public List<String> getChcekItem() { 
      return chcekItem; 
      } 


     public void setChcekItem(ArrayList<String> chcekItem) { 
      this.chcekItem = chcekItem; 
      } 


     public boolean isInput() { 
      return input; 
      } 

     public void setInput(boolean input) { 
       this.input = input; 
     } 

     public int getCount() { 
      return count-1; 
     } 


     public void setCount(int count) { 
     this.count = count; 
     } 

    } 
相關問題