通过javaScript将值传递给JSP [英] passing value to JSP via javaScript

查看:163
本文介绍了通过javaScript将值传递给JSP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

boolean canModify   = UTIL.hasSecurity("PFTMODFY") && something;

function name() {   
      I need to pass a value false to something when the page loads.   
}
window.onLoad = name

我如何传递一个值JavaScript加载到页面上的JSP

How can i pass a value from JavaScript to JSP on the page load

推荐答案

想想你的意思是另一种方式周围,​​让服务器端代码输出JavaScript在页面加载时可以看到的值。

I think you mean it the other way around, have server-side code output a value that JavaScript can see at page-load time.

只需输出就像其他任何东西一样,例如:

Just output it like you would anything else, e.g.:

<%
    boolean canModify   = UTIL.hasSecurity("PFTMODFY") && something;
%>
var canModify = <%=canModify%>;
//  ^              ^
//  |              +-- server-side variable
//  +-- client-side variable

JSP实际运行,返回给客户端的脚本代码将只是

When the JSP actually runs, the script code returned to the client will simply be

var canModify = false;

var canModify = true;

这是一个布尔值;如果你需要输出一个字符串,你需要在它周围加上引号,并注意你在其中转义应该在JavaScript文字字符串中转义的任何内容(例如反斜杠,以及你正在使用的任何引号字符) )。

That's a boolean; if you need to output a string, you need to put the quotes around it and be careful that you escape anything within it that should be escaped inside a JavaScript literal string (like a backslash, for instance, and whatever quote character you're using).

但是如果你真的意味着你想在页面加载时将值发送回服务器(这看起来很奇怪,但是嘿),你必须使用 Ajax 。如果你要做Ajax的话,我建议使用像 jQuery 这样的库,关闭原型 YUI ,或其他任何一个,因为它们可以大大简化您的流程。例如,使用jQuery,这个客户端代码将值发送回服务器:

But if you really mean you want to send a value back to the server on page load (which seems odd, but hey), you'd have to use Ajax for that. If you're going to be doing Ajax stuff, I'd recommend using a library like jQuery, Closure, Prototype, YUI, or any of several others as they can dramatically simplify the process for you. For instance, using jQuery, this client-side code sends a value back to the server:

jQuery.get("/your/page/url", {"name": "value"});

(或 jQuery.post 做出改变)。使用Prototype,它将是:

(or jQuery.post for things that make changes). Using Prototype, it'd be:

new Ajax.Request("/your/page/url", {
    method:     "GET", // or, of couse, "POST"
    parameters: {"name": "value"}
});

我上面提到的所有库都有类似的易于使用的机制,用于向服务器发送数据。请注意,此机制受同源策略的约束。

All of the libs I mentioned above have similarly easy-to-use mechanisms for sending data to the server. Note that this mechanism is bound by the Same Origin Policy.

你没有 需要 这个库,当然 —图书馆可以做的任何事情,你都可以自己做。我上面列出的功能都是 XMLHttpRequest 对象的包装器(维基百科 MSDN MDC )。请参阅链接的资源以获取示例。

You don't need a library for this, of course — anything a library can do, you can do yourself. The features I've listed above are all wrappers for the XMLHttpRequest object (Wikipedia, MSDN, MDC). See the linked resources for examples.

我不应该说你 需要 为此使用Ajax,更像是想要。 :-)如果由于某种原因,你真的不想使用Ajax,你可以通过用触发从服务器检索的JavaScript向页面添加内容,然后将你的值作为标记包含在该请求中来实现。例如:

I shouldn't have said you need to use Ajax for this, more like you want to. :-) If, for some reason, you really didn't want to use Ajax you could do it by adding something to the page with JavaScript that triggers a retrieval from the server, and then include your value as a flag in that request. For instance:

var link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.src = "/path/to/jsp?name=" + encodeURIComponent(value);
document.getElementsByTagName('head')[0].appendChild(link);

您的JSP会对查询字符串执行任何操作,然后返回任何有效的查询字符串样式表(例如 / * * / )。或者,如果您不想使用样式表链接,请使用 img 标记并返回透明像素;等等。

Your JSP would do whatever it needs to do with the query string, and then return anything that's valid in a stylesheet (/* */, for instance). Or if you don't want to use a style sheet link, use an img tag and return a transparent pixel; etc.

但是我不会这样做。我使用Ajax。

But I wouldn't do that. I'd use Ajax.

这篇关于通过javaScript将值传递给JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆