0

Is there any way to call a managed bean method from JavaScript?

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. I have tried to hide a button and make it submit on click of the link. But its not working.

enter image description here

animuson
  • 53,861
  • 28
  • 137
  • 147
raghav bhat
  • 57
  • 1
  • 10
  • Can you provide the specifications of the framework you are using ? – dShringi Feb 08 '13 at 09:20
  • Duplicate: [Question](http://stackoverflow.com/questions/3710908/jsf-2-0-ajax-call-a-bean-method-from-javascript-with-jsf-ajax-request-or-some) – dShringi Feb 08 '13 at 09:24
  • Using primefaces,jsf 2.0, and this may be a duplicate question. But the answer which they have given seems not working for me. Cant we simply call a managed bean method in javascript – raghav bhat Feb 08 '13 at 09:27
  • Have you tried using [``](http://www.primefaces.org/showcase/ui/remoteCommand.jsf) ? – dShringi Feb 08 '13 at 09:30

3 Answers3

0

To navigate to require page you need to pass the required information in f:param (e.g In your case pass the unique id or number or may be tender number something )and the call the ManagedBean method on it.

psi
  • 269
  • 2
  • 13
  • Its getting diverted to the proper page. Now i want to get the id in my managed bean and display documents related to the ID . so what should i do. – raghav bhat Feb 11 '13 at 06:17
  • Create on util class public class Utils { private static Utils dnbUtils; private Utils(){} public static Utils getInstance() { if(dnbUtils == null){ dnbUtils = new Utils(); } return dnbUtils; } public String getRequestParam(String parameterName) { FacesContext context = FacesContext.getCurrentInstance(); return (String)context.getExternalContext().getRequestParameterMap().get(parameterName); } } Managed Bean Code String value=Utils.getInstance().getRequestParam("name_of_f:param"); logger.info(""+value); – psi Feb 11 '13 at 06:45
  • ok , i used u r code and its working. I did FacesContext context = FacesContext.getCurrentInstance(); context.getExternalContext().getRequestParameterMap().get("tenderId");which gives me the required tender Id. But still i dont understand what is this Utils class. But Thanks . – raghav bhat Feb 11 '13 at 09:26
  • and i have not used the Utils class at all – raghav bhat Feb 11 '13 at 09:46
  • Basically Utils class is just a Utility class where we can specify JSF related functionality.Instead of implementing duplicate code in our ManagedBean we use to specify the functionlity in Utils class & simply create instance of the class and then access the method in ManagedBean. – psi Feb 11 '13 at 10:31
0

OK finally I got it .

In command link if I use action its not diverting. But I changed it to actionListener and it works.

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

                                     </p:commandLink>

and my Managed Bean method

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


}
raghav bhat
  • 57
  • 1
  • 10
0

Why not use simple <h:link> if all you want is to perform navigation on clicking? And you can set <f:param> to inform of the tender id or other unique values within the link. As your tender search results are located within <ui:repeat>, as I suppose, just add simple links to navigate to the tender docs. 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:

<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:

<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>.

Community
  • 1
  • 1
skuntsel
  • 11,624
  • 11
  • 44
  • 67
  • since i am using primefaces , i am not sure whether is that the reason. – raghav bhat Feb 11 '13 at 05:11
  • if i use h:link its displaying as a label and not as a link. and as for view docs .xhtml , i dont want to get the value in jsf. but i want the value in my java managed bean – raghav bhat Feb 11 '13 at 06:16
  • please also look at this code ' Its getting diverted to the proper page. Now i want to get the id in my managed bean and display documents related to the ID for which i have a query in MBean. so what should i do.' – raghav bhat Feb 11 '13 at 06:32
  • I wouldn't use button in your situation because all you want to do is perform navigation, as I stated in my answer. I think that get request is suitable in this case because user will be able to derive the tender he is viewing in the url. If you want to load a page on button click then you might use the following code: ` `. This will preset the value of a needed bean and make it available during view docs page load. – skuntsel Feb 11 '13 at 08:31
  • And don't forget to add ajax="false" to properties of commandButton. – skuntsel Feb 11 '13 at 08:38