简体   繁体   English

如何在Scriptlet中传递Javascript变量

[英]how to pass Javascript variable in Scriptlet

i desperately need some suggestion on how a javascript variable make available in Scriptlet. 我非常需要有关如何在Scriptlet中使用javascript变量的一些建议。 i know that javascript runs on client side where scriptlet code runs on the server. 我知道javascript在客户端运行,而scriptlet代码在服务器上运行。 But i think there is some way to make the javascript variable available in scriptlet. 但是我认为有一些方法可以使scriptlet中的javascript变量可用。 below is my code. 下面是我的代码。

Javascript fun JavaScript的乐趣

function showDirStructure(repoId, repoName){


        <% 
        String sr = repoId;
        if(sr!=null){
        JSONObject obj = getDirStructure.createJsonObject(request.getParameter("repoId")); 

         %>

The above javascript function (showDirStructure) takes two parameter. 上面的javascript函数(showDirStructure)具有两个参数。 i need to use those parameter on Scriplet tag to call createJsonObject (server side method). 我需要在Scriplet标记上使用这些参数来调用createJsonObject(服务器端方法)。

Scriptlet is executed before any data about webpage get sent from server to client. 在将有关网页的任何数据从服务器发送到客户端之前,将执行Scriptlet。 Whatever you want to do you need to send postback to server (with forms or ajax call). 无论您想做什么,都需要将回发发送到服务器(使用表单或ajax调用)。 I usually use jQuery so my answer will use it but feel free to modify it to use native JS code. 我通常使用jQuery,因此我的答案将使用它,但可以随时对其进行修改以使用本机JS代码。 First, I would create a page on server called createJsonObject, call it from client with $.ajax (type: "POST")and passed my two arguments as object {repoId: repoId, repoName: repoName}. 首先,我将在服务器上创建一个名为createJsonObject的页面,从客户端使用$ .ajax(类型:“ POST”)进行调用,并将我的两个参数作为对象{repoId:repoId,repoName:repoName}传递。 On server I would place my JSP on that page, read argumants upon page load, execute function and return object with data to client. 在服务器上,我将JSP放置在该页面上,在页面加载时读取argumant,执行函数并将带有数据的对象返回给客户端。 In .done() I would do something with that data (display them in form, save them in JS variables...). 在.done()中,我将对该数据进行一些处理(以表格形式显示它们,将其保存在JS变量中...)。 Hope this helps. 希望这可以帮助。

javascript variable available in scriptlet

NO. 没有。

Inside javascript ,you cannot call jsp ,The code with in the scriplets executes on serverside javascript内部,您无法调用jsp ,在脚本中使用的代码在服务器端执行

You need to either make a request to server(HTML forms/AJAX) for new content or Maintain Json object in client side itself while page loading and use it later. 您需要向服务器(HTML表单/ AJAX)发出请求以获取新内容,或者在页面加载时在客户端本身维护Json对象,并在以后使用它。

如果您打算解析JSON字符串,则也可以在客户端上进行解析。您可以使用jQuery.parseJSON( http://api.jquery.com/jQuery.parseJSON/ )或eval(不是eval虽然推荐)

您可以在第一次加载页面时向同一资源(JSP页面)发送请求,因此scriptlet可以使用javascript变量。

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

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