Contextmenu和目标与dataTable

时间:2014-07-25 01:44:51

标签: jsf primefaces datatable contextmenu target

我正在使用primefaces5.0进行jsf,这是我的情景:

我有一个页面包含一个带有contextmenu的dataTable,如下所示: enter image description here 当我点击上下文菜单时,我想弹出另一个"窗口" (不是" tab")包含有关所选数据的信息,例如String" data1"。

但是,无论我在action中使用url还是p:menuitem参数,都无法完成。

当我使用action参数时,anotherPage会在原始窗口中显示,而打开的新窗口为空: enter image description here

如果我将action="/anotherPage"更改为url="/anotherPage.xhtml"anotherPage会在新窗口中显示但没有关于所选数据的信息:(请注意,标题已更改为&#34 ;另一页")
enter image description here

这就是我所做的:

的facelet:

 <h:form>
        <p:contextMenu for="dataTable">
            <p:menuitem value="clickMe" icon="ui-icon-gear" 
                        onclick="window.open('', 'Popup', config = 'scrollbars=yes,status=no,toolbar=no,location=no,menubar=no,width=1300,height=370').focus();" 
                        target="Popup" actionListener="#{mainBean.showPopup()}" action="/anotherPage"/>
        </p:contextMenu>
        <p:dataTable id="dataTable" value="#{mainBean.dataList}" var="data" rowIndexVar="index" emptyMessage="Loading..." 
                     selectionMode="single" selection="#{mainBean.selectedStr}" rowKey="#{data}">
            <p:column headerText="data">
                <p:outputLabel value="#{data}"/>
            </p:column>
        </p:dataTable>    
    </h:form>

BackingBean:

private List<String> dataList=new ArrayList<>();
private String selectedStr="";

public void showPopup(){
    Map<String, Object> reqMap=FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
    reqMap.put("param", selectedStr);
}

AnotherPage.xhtml:

<p:outputLabel value="This is the selected String:"/>
<p:outputLabel value="#{anotherPageBean.txt}"/>

AnotherPageBean:

private String txt;    

@PostConstruct
public void init(){
    Map<String, Object> reqMap=FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
    txt=(String) reqMap.get("param");
}

非常感谢。


更新

我做了很多调查并发现了一些事情:

    不会为Target 呈现
  1. action menuitem的属性,而是known issue并且无法修复。
  2. Menuitem支持f:params根据optimus.prime传递参数。
  3. 当我使用url时,参数无法在请求地图中找到,这可能意味着它是一个全新的请求,因此我无法找到我放入的内容reqMap。
  4. 也许我可以在会话地图而不是请求地图中放置参数,因为我在3中提到过。
  5. 如果我使用action,则不会调用actionListenerurl

1 个答案:

答案 0 :(得分:0)

根据我在问题的更新部分中所说的,我找到了解决方法。
请注意,我相信这只是一种解决方法,希望有一些正常的解决方法。

由于菜单项的目标属性未针对操作呈现,因此我只能使用url。 所以我尝试使用f:params传递参数url,但仍然徒劳,因为我认为更新部分中提到的第3点。

之后,我尝试通过支持bean在会话映射中输入参数,并使用action来调用该方法。但是,它仍然无效,因为如果我使用action,则不会调用url

根据这个想法,我在menuitem之外调用该方法 我在dataTable添加了一个事件,并在那里调用方法:

<p:dataTable id="dataTable" value="#{mainBean.dataList}" var="data" rowIndexVar="index" emptyMessage="Loading..." 
                     selectionMode="single" selection="#{mainBean.selectedStr}" rowKey="#{data}">
        <p:ajax event="contextMenu" listener="#{mainBean.setParam()}"/>
        <p:column headerText="data">
            <p:outputLabel value="#{data}"/>
        </p:column>
</p:dataTable>    

在Backing Bean中:

 public void setParam(){
    HttpSession session=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    session.setAttribute("param", selectedStr);
}

所以我可以在anotherPageBean中通过会话获取信息,如:

 @PostConstruct
public void init(){
    HttpSession session=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    txt=(String) session.getAttribute("param");
    session.removeAttribute("param");
}

请注意,获取信息后应​​删除该属性 这就是结果:
enter image description here 只为那些有同样问题的人。