在 JavaScript 中获取请求属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7933866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:06:42  来源:igfitidea点击:

Get Request Attributes in JavaScript

javascriptjsp

提问by Snowy Coder Girl

When loading a page, I have the following in my controller:

加载页面时,我的控制器中有以下内容:

request.setAttribute("myAtt", "Testing");

I want to access this in my JSP file. In the HTML section, I am familiar with using things like:

我想在我的 JSP 文件中访问它。在 HTML 部分,我熟悉使用以下内容:

${myAtt} ${requestScope.myAtt}

and the like. However, I've never been sure how to access request parameters in JavaScript. I've tried a number of things, such as:

之类的。但是,我一直不确定如何在 JavaScript 中访问请求参数。我尝试了很多东西,例如:

var jsAtt = ${myAtt};
var jsAtt = '${myAtt}';
var jsAtt = eval(${myAtt});
var jsAtt = eval('${myAtt}');

etc, but nothing seems to work.

等等,但似乎没有任何效果。

Is there a way to grab request attributes via JavaScript? Please no jQuery, servlets, etc. I want pure JavaScript code.

有没有办法通过 JavaScript 获取请求属性?请不要使用 jQuery、servlet 等。我想要纯 JavaScript 代码。

I am kind of amazed I didn't find this already asked. So sorry if it is a duplicate and I just didn't see the orignal.

我有点惊讶我没有发现这个已经问过了。很抱歉,如果它是重复的,我只是没有看到原件。

回答by Snowy Coder Girl

Using the following should work.

使用以下应该有效。

var jsAtt = '${myAtt}';

I think I stumbled across issues because of trying to dynamically generate the string based off my needs, which JavaScript seems to not like. For example, this would have issues:

我想我偶然发现了一些问题,因为我试图根据我的需要动态生成字符串,而 JavaScript 似乎不喜欢这种情况。例如,这会有问题:

var counter = 1;
var jsAtt = '${myAtt' + counter + '}';

JavaScript seems to recognize the request parameter syntax, but only if it's completely predetermined.

JavaScript 似乎可以识别请求参数语法,但前提是它是完全预先确定的。

回答by Mechkov

For GET requests you can get the values from the QueryString. Check this page: http://www.thimbleopensource.com/tutorials-snippets/get-request-parameters-using-javascript

对于 GET 请求,您可以从 QueryString 获取值。检查此页面:http: //www.thimbleopensource.com/tutorials-snippets/get-request-parameters-using-javascript

I am not sure about POST requests though.

不过,我不确定 POST 请求。

回答by Laurent

Put the following code inside the head tagof your jsp page (not in a js file) :

将以下代码放在您的 jsp 页面的 head 标记中(而不是在 js 文件中):

<script type="text/javascript">
var jsAtt = '<%= request.getAttribute("myAtt") %>';
</script>

But your string must NOT contains the simple quote because it will break the interpretation of the String in the jsp.

但是您的字符串不能包含简单的引号,因为它会破坏 jsp 中字符串的解释。

If you need String with simple quote, you will need, for example, to replace them on the server side by "&#39;" :

如果您需要带简单引号的字符串,例如,您需要在服务器端用“'”替换它们 :

request.setAttribute("myAtt", myAtt.replaceAll("'", "&#39;"));

And replace it again in your JSP :

并在您的 JSP 中再次替换它:

<script type="text/javascript">
var jsAtt = '<%= request.getAttribute("myAtt") %>';
jsAtt = jsAtt.replace(/&#39;/g, "'");
</script>

For Object instead of String, you can use JSON to pass them as a String.

对于 Object 而不是 String,您可以使用 JSON 将它们作为 String 传递。

回答by pacman

I have not tested this but have you tried this?

我没有测试过这个,但你试过这个吗?

${pageContext.request.myAtt}