How to call managed bean methods with parameters via JavaScript

后端 未结 2 1763
感动是毒
感动是毒 2020-12-09 06:53

I am developing a web application and I use JSF and PrimeFaces frameworks and external Geo Map API.

The Map API gives me POI_id when I clicked on a POI

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-09 07:28

    You can use PrimeFaces remote command component ().

    RemoteCommand enables executing backing bean methods and do partial update triggered by custom client side script.

    Add it to the view it in a following way:

    
    

    And use it in Javascript like so:

    
    

    or call it from an event handler like so:

    ...

    If you additionally want to pass parameters to the server make a following call:

    
    

    The listener could be like:

    public void listen(){
        FacesContext context = FacesContext.getCurrentInstance();
        Map params = context.getExternalContext().getRequestParameterMap();
        System.out.println(params.get("param1"));
        System.out.println(params.get("param2"));
    }
    

    One of the comments asked to return a Value to Javascript.
    Well in that case you can use Primeface's Request Context's execute() method to execute any javascript you want.

    RequestContext.getCurrentInstance().execute("your javascript code");
    

提交回复
热议问题