【问题标题】:Contextmenu and target with dataTable带有数据表的上下文菜单和目标
【发布时间】:2014-07-25 01:44:50
【问题描述】:

我正在使用 primefaces5.0 做 jsf,这是我的场景:

我有一个页面包含一个带有上下文菜单的数据表,如下所示: 当我单击上下文菜单时,我想弹出另一个“窗口”(不是“选项卡”),其中包含有关所选数据的信息,例如字符串“data1”。

但是,无论我在p:menuitem 中使用action 还是url 参数,都无法做到这一点。

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

如果我将action="/anotherPage" 更改为url="/anotherPage.xhtml"anotherPage 会在新窗口中显示,但没有有关所选数据的信息:(请注意,标题已更改为“另一页”)
@ 987654325@

这是我所做的:

小脸:

 <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);
}

另一个页面.xhtml:

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

另一个PageBean:

private String txt;    

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

非常感谢。


更新

我做了很多调查并发现了一些东西:

  1. action 的 menuitem 的Target 属性不会呈现,它是known issue,不会被修复。
  2. Menuitem支持f:params根据optimus.prime传递参数。
  3. 当我使用url时,在request map中找不到参数,这可能意味着这是一个全新的请求,所以我在reqMap中找不到。
  4. 也许我可以将参数放在会话映射中而不是请求映射中,因为我在 3 中提到过。
  5. 如果我使用url,则不会调用actionactionListener

【问题讨论】:

  • 您是否尝试过在您的menuItem 上使用target="_blank"
  • 我试过了,页面仍然显示在原来的窗口中......

标签: jsf primefaces datatable contextmenu target


【解决方案1】:

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

由于 menuitem 的目标属性未呈现用于操作,我只能使用url。 所以我尝试使用f:params 来传递参数url 但仍然是徒劳的,因为我认为我在 update 部分中提到的第三点。

之后,我尝试通过backing bean将参数放入会话映射中,并使用action调用该方法。但是,它仍然不起作用,因为如果我使用 url,则不会调用 action

按照这个思路,我调用了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");
}

请注意,获取信息后该属性应被删除。
结果如下:
只为那些有同样问题的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多