1

我正在制作一个公交路线项目,我需要谷歌地图来确定车站和停靠点,我从 JavaScript 中的谷歌地图获取坐标,现在我需要将这些坐标传递给我可以制作的 java 类这些坐标上的不同过程。我在 netbeans 上使用 JSF -Java server faces-。谁能帮我将这些坐标传递给 .java 类?提前致谢

4

3 回答 3

1

有许多框架可以帮助您解决这个问题。例如,Primefaces 在其新的 JSF 实现中内置了一个谷歌地图插件(http://www.primefaces.org/showcase-labs/ui/gmapHome.jsf)。手动操作也很容易。只需设置一个 Servlet 来处理 GET 请求并使用您希望将数据发送到 Servlet 的任何 Ajax 方法。我将从寻找一些 Servlet 和 Ajax 示例开始。同样,根据您使用的 JSF 实现,可能已经内置了 Ajax 工具。

祝你好运。

分享和享受。

于 2012-11-30T21:24:25.020 回答
0

You can use <a4j:jsFunction> to pass javascript values to the managed bean. This is an example.

This is your js array

<script>
  var coordinateArray = [12, 26];
</script>

This is your page. Note that sendData is the name of your jsFunction and coordinateArray.join() converts the array to a String.

   <h:form>
    <a4j:commandButton value="Send" onclick="sendData(coordinateArray.join())"/>
    <a4j:jsFunction name="sendData">
      <a4j:actionparam name="param1" assignTo="#{hBean.coordinatesString}" />
    </a4j:jsFunction>
   </h:form>

In you managed bean

  String coordinatesString;
  String[] coordinatesArray;

  public String getCoordinatesString() {
    return coordinatesString;
  }

  public void setCoordinatesString(String coordinatesString) {
    this.coordinatesString = coordinatesString;
    this.coordinatesArray = coordinatesString.split(",");//This is what you need
  }

Edit:


Think a4j:jsFunction as a normal javascript function.You can put an actionParam inside it as in above sample. If so, it means that jsFunction has one argument(similar to normal javascript function argument). You give the jsFunction a name, and call it, using that given name like a normal javascript function(i.e. funcName()). If there is an actionparam inside it you should pass a parameter when you calling it(i.e. funcName(value)).
A <h:form> should not be necessarily around it. But if you want to call it when you click a commandButton, that button should be within a form.
As you say in your comment, if the name of your coordinate array is path then you call the above jsFunction like this. sendData(path.join()). You don't add any javascript code inside the jsFunction. Simply you call the jsFunction from your javascript code as you call a normal javascript function.

于 2012-12-01T00:15:04.987 回答
0
  1. 使用隐藏输入<h:inputHidden value="#{bean.value}"/>
  2. 使用 javascript 更新其值。
  3. <h:inputHidden value="#{bean.value}"/>更新其 bean 值的过程。

这是一个工作示例:

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class Bean {

    private String value;

    @PostConstruct
    public void postConstruct() {
        value = "SERVER SIDE VALUE";
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}
<h:head>
    <script>
        function updateElementValue(id,value){
            document.getElementById(id).value = value;
        }
    </script>
</h:head>

<h:body>

    <h:form id="form" prependId="false">

        <p:commandButton value="UPDATE CLIENT SIDE VALUE OF INPUT-HIDDEN"
                         onclick="updateElementValue('nameInputHiddenId',
                                                     'CLIENT SIDE VALUE');
                                                      return false;"/>
        <p:commandButton value="UPDATE SERVER SIDE VALUE OF INPUT-HIDDEN" 
                         process="@form" 
                         update="dialogId" 
                         oncomplete="dialogWidgetVar.show();" />

        <h:inputHidden id="nameInputHiddenId" value="#{bean.value}" />

        <p:dialog id="dialogId" widgetVar="dialogWidgetVar">
            <h:outputText id="nameOutputTextId" value="#{bean.value}" />
            <p:commandButton value="Yes" onclick="dialogWidgetVar.hide();" />
            <p:commandButton value="No" onclick="dialogWidgetVar.hide();"/>
        </p:dialog>

    </h:form>

</h:body>
于 2012-11-30T22:55:29.617 回答