2012-01-28 59 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");