如何将bean中的数组返回到javascript中?

我正在开发一个应用程序,它使用javascript中的GoogleMapAPI,但是我不知道如何将bean中的数组返回到javascript中。任何建议都将不胜感激!

What JSF framework are you using? Have you seen primefaces? Check out gMap http://www.primefaces.org/showcase-labs/ui/gmapHome.jsf

You can simply generate the JavaScript in your bean and output it from a variable example :

@ManagedBean
@RequestScoped
public class Bean
{
    public String getMyJavaScriptArray()
    {
        // Your business logic to generate the array
        return "var mycars = new Array(); mycars[0] = \"Saab\"; mycars[1] = \"Volvo\"; mycars[2] = \"BMW\";";
    }
}

<script>
    <h:outputText value="#{bean.myJavaScriptArray}" escape="false" />
</script>

Or you can use this JSF GMap component :

http://code.google.com/p/gmaps4jsf/

In your managed bean return the values as a comma seperated String.

public String getNames() {
  return "Thomson,John,William";
}

In your javascript, access it using EL and convert it again to an array as below.

var str = "#{myBean.text}";
var jsArray = str.split(","); //This is what you need