简体   繁体   English

JSF从JavaScript调用托管bean方法

[英]JSF to call managed bean method from JavaScript

Is there any way to call a managed bean method from JavaScript? 有什么方法可以从JavaScript调用托管bean方法吗?

I have a link in a datatable which should go to a document repository for the corresponding tender. 我在数据表中有一个链接,该链接应转到相应招标的文件存储库。 Please check the screenshot. 请检查屏幕截图。 But the datatable, inside a panel, which is inside a layout and so the link is not diverting the page to the required path. 但是数据表位于面板内部,面板位于布局内,因此链接不会将页面转移到所需的路径。 So I am trying to call a JavaScript from the link which in turn call a method from managed bean which will return to the required page. 因此,我试图从链接中调用JavaScript,而该链接又从托管Bean中调用方法,该方法将返回所需的页面。 I have tried to hide a button and make it submit on click of the link. 我试图隐藏一个按钮,并使其在单击链接时提交。 But its not working. 但是它不起作用。

在此处输入图片说明

要导航到要求页面,您需要在f:param中传递所需的信息(例如,在您的情况下,传递唯一的ID或数字或可能是投标编号的东西)并在其上调用ManagedBean方法。

OK finally I got it . 好吧,终于我明白了。

In command link if I use action its not diverting. 在命令链接中,如果我使用动作,则不会转移。 But I changed it to actionListener and it works. 但是我将其更改为actionListener,并且可以工作。

 <p:commandLink id="submitButton" actionListener="#{docManagedBean.viewDocs(action)}" 
                                                     value="View Docs">

                                     </p:commandLink>

and my Managed Bean method 和我的Managed Bean方法

 public void viewDocs(ActionEvent action) throws IOException{
 FacesContext ctx = FacesContext.getCurrentInstance();
FacesContext.getCurrentInstance().getExternalContext().redirect("/Proj-war/faces" +      "/DocumentRepositary/documentsView" + ".xhtml");


}

Why not use simple <h:link> if all you want is to perform navigation on clicking? 如果只想在单击时执行导航,为什么不使用简单的<h:link> And you can set <f:param> to inform of the tender id or other unique values within the link. 您可以设置<f:param>来通知链接中的投标ID或其他唯一值。 As your tender search results are located within <ui:repeat> , as I suppose, just add simple links to navigate to the tender docs. 正如我想的那样,您的投标搜索结果位于<ui:repeat>内,只需添加简单链接即可导航至投标文档。 My point of view is that making a redirect from an action listener method is actually a big problem in design. 我的观点是,从动作侦听器方法进行重定向实际上是设计中的大问题。 Usage of command buttons is acceptable if you want to perform some operations on the page or before viewing the target page. 如果要在页面上或在查看目标页面之前执行某些操作,则可以使用命令按钮。

Simple example: part of search.xhtml: 简单示例:search.xhtml的一部分:

<ui:repeat var="tender" value="#{tenderBean.tenders}">
    <!-- UI part of table -->
    <h:link value="View tender docs"utcome="path/to/tender/docs/viewDocs.xhtml">
        <f:param name="tenderId" value="#{tender.id}" />
    </h:link>
</ui:repeat>

Simple example continued: part of viewDocs.xhtml: 继续简单的示例:viewDocs.xhtml的一部分:

<f:metadata>
    <f:viewParam name="tenderId" value="#{tenderDocsBean.tenderId}" required="true" />
    <f:event type="preRenderView" listener="#{tenderDocsBean.loadTenderDocs}" />
</f:metadata>

Look at this answer on usage of <f:viewParam> . 查看有关<f:viewParam>用法的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM