如何将数据从 Struts 操作发送到 javascript?
我正在尝试使用 Struts 2 和 javascript 创建 webb 应用程序,但将操作中的数据传递到 javascript 时遇到一些问题。
这是我试图发送/访问的列表:
List<MarkerClass> markers;
MarkerClass 是根据亲爱的定义的:
final class MarkerClass
{
public Integer objectId;
public String street;
public String streetNumber;
public String zip;
public String city;
public Integer statusId;
public Float lattitude;
public Float longitude;
}
该操作还包括标记的 getter:
public List<MarkerClass> getMarkers()
{
return markers;
}
在我的 jsp 文件中,我尝试这样做:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function initialize()
{
var titel = "";
for(var i, "${markers}")
{
titel = titel+ "${markers[i].street}";
}
}
浏览器将“${markers}”替换为“[se.fubar.test.MarkerClass@37a4,se.fubar.test.MarkerClass@9ad97c]”
我猜想有一种更好、更聪明的方法来做到这一点,但由于我对 Struts 有点陌生,并且在偏头痛的影响下尝试编码,所以答案让我困惑。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能只在 javascript 函数中使用 struts 变量并期望它能够工作。请记住,
${...}
内容会在页面的 HTML 发送到浏览器之前进行处理。当 JavaScript 在浏览器中呈现时,您只剩下文本表示形式。你需要做的是类似的事情(检查语法,我还没有使用过这个东西):无论如何,沿着这些思路......基本上你的浏览器看到的Javascript看起来像
我希望这是有道理的,并且是与你所问的内容相关。
顺便说一句,通常有更好的方法来完成此功能,例如构建动态 js 等。也许有您可以使用的内置 Struts 2 组件?
You cannot just use a struts variable in a javascript function and expect it to work. Remember that the
${...}
stuff gets processed before the HTML for the page is sent to the browser. By the time the javascript is rendered at the browser, you are only left with the textual representations. What you will need to do is something like (check the syntax, I haven't used this stuff i a while):Something along those lines anyway... Basically the Javascript seen by your browser will look like
I hope that makes sense and is related to what you were asking.
By the way, there are usually better ways of accomplishing this functionality that building dynamic js etc. Probably there are built in Struts 2 components that you can use?
您必须在请求或会话中设置该变量,然后使用 jsp 标记访问它,如下所示,
然后在 js 中使用 var 。
如果您在请求或会话中设置对象,则必须使用 get 方法来访问属性的值,然后像这样使用它:
var myVar= '';
(假设你的getter方法是getAttribute)
不可能访问session中的数据或直接从js请求
希望这有帮助
you would have to set that variable in request or in session and then access it using a
<c:out
jsp tag like sothen use the var inside your js.
In case you set an object in request or session you have to use the get method to access the value of an attribute then use it like so:
var myVar= '<c:out value="${requestScope.myObj.attribute}"/>';
(assuming you getter method is getAttribute)
it is not possible to access data in session or request directly from js
hope this helps
您可以在服务器上将对象转换为 json (请参阅 http://www.json.org/java /index.html ),然后对字符串调用 eval() 以获取对象的 javascript 表示形式。
You could convert the object to json on the server (see http://www.json.org/java/index.html ) and then call eval() on the string to get a javascript representation of your object.
你可以尝试像这样访问它。
但是你必须使用循环来从列表中获取每个对象,将其放在值堆栈上,然后获取它的每个对象。否则你可以要求 ognl 为你做这件事。就像
尝试一下,因为 OGNL 有能力访问值栈上的对象
you can try accessing it something like this.
but you have to use a loop to fetch each object from the list place it on the value stack and than fetch each object of it.else you can ask ognl to do it for you.something like
just try it since OGNL has capacity to access the object on the value stack