Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jsf - How to call managed bean methods with parameters via JavaScript

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 on the map. But it's not enough for me, I want to get information about POI from a servlet and display it in a pop-up window. (fields like address, name, phone number, etc.).

So, I need to send an HTTP request to the servlet in my managed bean when I click the POI on the map.

I can get poi_id, but I cannot send this id to the backend managed bean, so getting the POI information does not seem possible.

How can I send poi_id to my managed bean and handle the response to display in a popup window?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use PrimeFaces remote command component (<p:remoteCommand>).

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:

<p:remoteCommand name="myRemote" actionListener="#{myBean.listen}"/>

And use it in Javascript like so:

<script type="text/javascript">
   myRemote(); //makes a remote call
</script>

or call it from an event handler like so:

<div onclick="myremote();">...</div>

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

<script type="text/javascript">
   myRemote([{name:'param1', value:150}, {name:'param2', value:220}]); //makes a remote call with parameters
</script>

The listener could be like:

public void listen(){
    FacesContext context = FacesContext.getCurrentInstance();
    Map<String,String> 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");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...