在JavaScript中获取请求属性 [英] Get Request Attributes in JavaScript

查看:71
本文介绍了在JavaScript中获取请求属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

${myAtt} ${requestScope.myAtt}

等。但是,我从未确定如何在JavaScript中访问请求参数。我尝试了很多东西,例如:

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:

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

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

etc, but nothing seems to work.

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

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

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

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.

推荐答案

使用以下内容应该有效。

Using the following should work.

var jsAtt = '${myAtt}';

我认为我偶然发现问题是因为尝试根据我的需要动态生成字符串,这是JavaScript似乎不喜欢。例如,这会有问题:

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:

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

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

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

这篇关于在JavaScript中获取请求属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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