简体   繁体   English

在Java后端和javascript前端之间传输数据

[英]transferring data between java backend and javascript frontend

I'm creating a Java EE web application. 我正在创建一个Java EE Web应用程序。 This application uses Java as the server side programming language and uses Javascript for the client side. 此应用程序使用Java作为服务器端编程语言,并使用Javascript作为客户端。 The Javascript is in an HTML5 document (needs to be for a specific reason). 该Javascript位于HTML5文档中(出于特定原因需要)。 My Javascript needs some data from the server in order to perform its task. 我的Javascript需要来自服务器的一些数据才能执行其任务。 Which leaves me with my problem: how can I fill a Javascript variable with data from the Java server? 我的问题是:我如何用Java服务器中的数据填充Javascript变量?

I was looking into using JSF and incorporating the HTML5 with the Javascript into it. 我一直在研究使用JSF,并将HTML5与Javascript集成到其中。 Apparently, the server side code is rendered first and then sent to the client. 显然,首先呈现服务器端代码,然后将其发送给客户端。 In that case, I can do something like this: 在这种情况下,我可以执行以下操作:

var data = #{controller.getData};

If this does work, there is one problem: the data is "static". 如果这样做有效,则存在一个问题:数据是“静态”的。 Whereas, I need to be able to change this data frequently by receiving new data from the server. 而我需要能够通过从服务器接收新数据来频繁地更改此数据。 But, if the server side code is already rendered and then sent to the client, each call to #{controller.getData} will return the same value. 但是,如果已经渲染了服务器端代码,然后将其发送到客户端,则对#{controller.getData}每次调用都将返回相同的值。

So, this lead me to look into AJAX. 因此,这使我开始研究AJAX。 JSF provides an AJAX Javascript library, however, I can't seem to understand how to use it to change a variable within my Javascript code in the HTML5 document (within the JSF page). JSF提供了一个AJAX Javascript库,但是,我似乎无法理解如何使用它来更改HTML5文档(在JSF页面中)的Javascript代码中的变量。 So, again, how can I fill a Javascript variable with data from the Java server? 因此,再次如何用Java服务器中的数据填充Javascript变量?

If you'r using JSF without any component library, you can embed your script into a panelGroup and rerender this panel by ajax: 如果使用不带任何组件库的JSF,则可以将脚本嵌入到panelGroup中,并通过ajax重新呈现此面板:

<h:inputText id="mInput" value="#{myBean.name}">

    </h:inputText>
    <h:commandButton value="Do it"  action="#{myBean.doit}">
        <f:ajax render="thePanel" />
    </h:commandButton>


    <h:panelGroup id="thePanel">
        <h:outputText value="#{myBean.name}" />

        <script>
            var myVar = '#{myBean.name}';
            alert(myVar);
        </script>
    </h:panelGroup>

But if you have Primefaces on you can call a javascript on your controller, and it will be executed by the browser once it gets the response: 但是,如果您具有Primefaces,则可以在控制器上调用javascript,一旦获得响应,它将由浏览器执行:

RequestContext.getCurrentInstance().execute("alert('test');");

An other option available with Primefaces also is to use oncomplete attribute: Primefaces可用的另一个选项也是使用oncomplete属性:

<p:commandButton value="Do it" action="#{myBean.doIt}" oncomplete="alert('test');" />

I think the easiest way for you to do this is to define a hidden variable: 我认为最简单的方法是定义一个隐藏变量:

<h:inputHidden id="hiddenData" value="#{controller.getData}" />

Then in Javascript you can easily do: 然后,使用Javascript,您可以轻松地做到:

var data = $('#hiddenData').val(); //using JQuery
var data = document.getElementByID("hiddenData").value();

Note that you will have to re-render this hidden value on you AJAX action. 请注意,您将必须在AJAX操作上重新渲染此隐藏值。 Also, you can take a look at Ajax Push library available with Richfaces. 另外,您可以看一下Richfaces提供的Ajax Push库。 Hope it helps. 希望能帮助到你。

What I believe I was looking for was a Web Service, such as, SOAP and REST. 我相信我一直在寻找的是Web服务,例如SOAP和REST。 It would be useful to use AJAX embedded in a JSF component if I wanted to update that portion of the view. 如果我想更新视图的那部分,使用嵌入在JSF组件中的AJAX会很有用。 However, I merely need data from the server for a specific reason. 但是,我仅出于特定原因需要来自服务器的数据。 So, it seems I can use a Web Service for this case. 因此,在这种情况下,似乎可以使用Web服务。 I can use the @path annotation in a singleton or stateless EJB to make it a REST URI endpoint. 我可以在单例或无状态EJB中使用@path批注,以使其成为REST URI端点。 Then, in the Javascript code, I can access that URI endpoint through an AJAX call which will allow me to access a method and receive a response from the EJB. 然后,在Javascript代码中,我可以通过AJAX调用访问该URI端点,这将允许我访问方法并从EJB接收响应。

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

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