简体   繁体   English

我如何在Scriplets中使用javascript var

[英]how can i use javascript var in scriplets

I am trying to learn jsp. 我正在尝试学习jsp。 I know a little bit java and I dont know much about html tags so I simple use java codes as much as I can. 我对Java有一点了解,而对html标签了解不多,所以我会尽可能简单地使用Java代码。 What I am trying to do there is getting data from variables from text boxes and using them as string. 我要在那里做的是从文本框中的变量获取数据并将其用作字符串。

var text1 =<% request.getParameter("locationId"); %>;

<%
if ((text1 != null && text2 != null) && (!text1.equals("") && !text2.equals(""))) {
        kw1 = "'%"+text1+"%'";
        kw2 = "'%"+text2+"%'";
.
.
.
}
%>

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 something like createJsonObject , call it from client with $.ajax (type: "POST") and passed my argument as object 首先,我将在服务器上创建一个名为createJsonObject类的createJsonObject ,使用$.ajax (type: "POST")从客户端调用它,并将参数作为对象传递

{varID: varID}

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 you out. 希望这可以帮助你。

Example (Just showing how you can use Ajax with form example) 示例(仅显示如何将Ajax与表单示例一起使用)

HTML form: HTML形式:

<form name="formName" method="post" action="">
 <input type="text" name="name" id="firstName" value="" />
 <input type="text" name="lastName" id="lastName" value="" />
 <input type="submit" name="Update" id="update" value="Update" />
</form>

Ajax Post: 阿贾克斯邮报:

$("#update").click(function(e) 
{
   e.preventDefault();
   var firstName = $("#firstName").val(); 
   var lastName = $("#lastName").val();
   var dataObject = {};
   dataObject.firstName = firstName;
   dataObject.lastName = lastName;

   $.ajax({
       type:'POST',
       data:dataObject,
       url:'returnData.php',
       success:function(data) 
       {
         alert(data);
       }
   });
});

PHP: PHP:

<?php
  $receivedObject = json_decode($_POST['data'], true);
  $name = $receivedObject['firstName'];
  $lastName = $receivedObject['lastName'];
  echo $name . ' ' . $lastName;
?>

I've not test this, so there might be somewhere i've gone wrong. 我尚未对此进行测试,因此可能存在我出错的地方。 But try something like my example and just ask if you need any help. 但是尝试类似我的示例的方法,然后问是否需要任何帮助。

Ali, you can not use a javascript variable into jsp scriplate. 阿里,您不能在jsp scriplate中使用javascript变量。

<%

String locationId=request.getParameter("locationId"); 

if ((text1 != null && text2 != null) && (!text1.equals("") && !text2.equals(""))) {
        kw1 = "'%"+text1+"%'";
        kw2 = "'%"+text2+"%'";
.
.
.
}
%>

but vise versa is possible you can use JSP variable into you javascript code.like this. 但是反之亦然,您可以在JavaScript代码中使用JSP变量。

<script>
    var locationId='<%=request.getParameter("locationId")%>';
    alert(locationId);
</script>

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

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