我正在使用primefaces5.0进行jsf,这是我的情景:
我有一个页面包含一个带有contextmenu的dataTable,如下所示:
当我点击上下文菜单时,我想弹出另一个"窗口" (不是" tab")包含有关所选数据的信息,例如String" data1"。
但是,无论我在action
中使用url
还是p:menuitem
参数,都无法完成。
当我使用action
参数时,anotherPage
会在原始窗口中显示,而打开的新窗口为空:
如果我将action="/anotherPage"
更改为url="/anotherPage.xhtml"
,anotherPage
会在新窗口中显示但没有关于所选数据的信息:(请注意,标题已更改为&#34 ;另一页")
这就是我所做的:
的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
呈现action
menuitem的属性,而是known issue并且无法修复。f:params
根据optimus.prime传递参数。url
时,参数无法在请求地图中找到,这可能意味着它是一个全新的请求,因此我无法找到我放入的内容reqMap。action
,则不会调用actionListener
或url
。答案 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");
}
请注意,获取信息后应删除该属性
这就是结果:
只为那些有同样问题的人。