简体   繁体   English

将JS变量传递给Java类

[英]passing JS variable to a java class

I am making a transit routing project and I needed google maps to pin out the stations and the stops,,, I get the coordinates from google maps in a JavaScript,, now I need to pass these coordinates to a java class where I can make different processes on these coordinates. 我正在做一个公交路线规划项目,我需要使用Google Maps来确定车站和车站,我用JavaScript从Google Maps中获取坐标,现在我需要将这些坐标传递给Java类,在其中我可以这些坐标上的不同过程。 I am using JSF -Java server faces- on netbeans. 我在Netbeans上使用JSF -Java服务器Faces-。 Can anyone help me with passing these coordinates to a .java class? 谁能帮助我将这些坐标传递给.java类? Thanks in advance 提前致谢

There are many frameworks that will help you with this. 有很多框架可以帮助您。 Primefaces, for example, has a google maps plugin built right in to their new JSF implementation ( http://www.primefaces.org/showcase-labs/ui/gmapHome.jsf ). 例如,Primefaces在其新的JSF实现中内置了一个google maps插件( http://www.primefaces.org/showcase-labs/ui/gmapHome.jsf )。 Doing it by hand is also pretty easy. 手工操作也很容易。 Just set up a Servlet to handle GET requests and use whatever Ajax method you would like to send the data to your Servlet. 只需设置一个Servlet来处理GET请求并使用您想将数据发送到Servlet的任何Ajax方法。 I'd start by looking for some Servlet and Ajax examples. 我将从寻找一些Servlet和Ajax示例开始。 Again, depending on what implementation of JSF you're using there may be Ajax tools built in already. 同样,根据您使用的JSF实施方式,可能已经内置了Ajax工具。

Best of luck. 祝你好运。

Share and enjoy. 分享并享受。

  1. Use a hidden input <h:inputHidden value="#{bean.value}"/> . 使用隐藏的输入<h:inputHidden value="#{bean.value}"/>
  2. Update its value using javascript. 使用javascript更新其值。
  3. Process <h:inputHidden value="#{bean.value}"/> to update its bean value. 处理<h:inputHidden value="#{bean.value}"/>以更新其bean值。

Here is a working example: 这是一个工作示例:

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>

You can use <a4j:jsFunction> to pass javascript values to the managed bean. 您可以使用<a4j:jsFunction>javascript值传递给托管bean。 This is an example. 这是一个例子。

This is your js array 这是你的js数组

<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. 请注意, sendData是您的jsFunction的名称, coordinateArray.join()会将数组转换为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 在您管理的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. 可以将a4j:jsFunction视为普通的javascript函数。您可以像上面的示例一样在其中放置一个actionParam If so, it means that jsFunction has one argument(similar to normal javascript function argument). 如果是这样,则意味着jsFunction有一个参数(类似于普通的javascript函数参数)。 You give the jsFunction a name, and call it, using that given name like a normal javascript function(ie funcName() ). 您给jsFunction一个名称,然后像普通的javascript函数(即funcName() )一样使用给定的名称进行调用。 If there is an actionparam inside it you should pass a parameter when you calling it(ie funcName(value) ). 如果其中有一个actionparam ,则在调用它时应该传递一个参数(即funcName(value) )。
A <h:form> should not be necessarily around it. <h:form>不一定必须围绕它。 But if you want to call it when you click a commandButton , that button should be within a form. 但是,如果要在click commandButton时调用它,则该按钮应该在表单内。
As you say in your comment, if the name of your coordinate array is path then you call the above jsFunction like this. 正如您在评论中所说,如果坐标数组的名称是path则可以像上面那样调用上面的jsFunction sendData(path.join()) . sendData(path.join()) You don't add any javascript code inside the jsFunction . 您无需在jsFunction添加任何javascript代码。 Simply you call the jsFunction from your javascript code as you call a normal javascript function. 您只需像调用普通javascript函数一样,从javascript代码中调用jsFunction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM