2012-01-28 57 views
1

我使用Primefaces 3.0.1並用編程式填充模型構建了一個菜單欄。我需要一些鏈接,如depotDetails.xhtml? ID = 1但是,如果使用這些URL我的菜單項如何正確添加SetPropertyActionListener?

item.setUrl("depotDetail.xhtml?id=1"); // that dont work 

,所以我嘗試添加一個ActionListener:

FacesContext facesContext = FacesContext.getCurrentInstance(); 
ValueExpression target =  facesContext.getApplication().getExpressionFactory().createValueExpression(FacesContext.getCurrentInstance().getELContext(), "#{DepotBean.currentDepot}",String.class); 
ValueExpression value = facesContext.getApplication().getExpressionFactory().createValueExpression(FacesContext.getCurrentInstance().getELContext(), "ehnemeneee",String.class); 

ActionListener handler = new SetPropertyActionListenerImpl(target, value); 

item.addActionListener(handler); 

但也不要工作。任何人都可以幫忙嗎?

Greets Thomas

+0

你是什麼意思item.setUrl(「depotDetail.xhtml?id = 1」); //不工作?網址沒有被調用,或者它不包含任何網址或不完整的網址? – 2012-01-28 07:38:25

+0

你試過item.setUrl(「depotDetail.jsf?id = 1」); ? – Daniel 2012-01-28 08:57:30

+0

是的,我嘗試過,但它只通過depotDetail.xhtml不帶參數 – thomas 2012-01-28 11:23:39

回答

0

我得到了一個解決方案......但它是正確的方式?

// building the menu model .. 

UIParameter param = null; 

for(Depots testdepot: depotList){ 
    param = new UIParameter(); 
    param.setName("currentDepotId"); 
    param.setValue(testdepot.getIdDepot()); 

    item = new MenuItem();    
    item.setValue(testdepot.getDepotName()); 
    // calling menuListener 
    item.addActionListener(new MenuListener());    
    item.setUpdate("messages"); 
    item.setId("menuItemDepot"+testdepot.getIdDepot()); 
    item.getChildren().add(param); 

    submenu.getChildren().add(item); 
} 

// MenuListener 
public void processAction(ActionEvent event) throws AbortProcessingException { 
    FacesContext fc = FacesContext.getCurrentInstance(); 
    HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest(); 
    HttpSession session = (HttpSession) fc.getExternalContext().getSession(false); 

    // get param id 
    String name = (String) event.getComponent().getAttributes().get("currentDepotId"); 

    // set session var 
    session.setAttribute("currentDepotId", name); 

    fc.getExternalContext().redirect(request.getContextPath() + "/userarea/depotDetail.xhtml"); 
} 

// read the session in the depot bean 
FacesContext fc = FacesContext.getCurrentInstance(); 
HttpSession session = (HttpSession) fc.getExternalContext().getSession(false); 
String currentDepotId = session.getAttribute("currentDepotId");