如何将数据从 Struts 操作发送到 javascript?

发布于 2024-10-31 12:42:19 字数 1204 浏览 1 评论 0 原文

我正在尝试使用 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 有点陌生,并且在偏头痛的影响下尝试编码,所以答案让我困惑。

I'm trying to create a webb application using Struts 2 and javascript and I'm having some trouble passing data from my action into my javascript.
This is the list I'm trying to send/access:

List<MarkerClass> markers;  

MarkerClass is defined acoprding to belove:

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;  
}

The action also includes a getter for markers:

public List<MarkerClass> getMarkers()
{
    return markers;
}

In my jsp-file I have tried doing this:

<%@ 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}";
    }
}

The browser substitutes "${markers}" with "[se.fubar.test.MarkerClass@37a4, se.fubar.test.MarkerClass@9ad97c]"
I'm guessing there is a better and smarter way to do this but since I'm a bit new to Struts and is trying to code while under the influence of a migrane the answer elludes me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

顾冷 2024-11-07 12:42:19

您不能只在 javascript 函数中使用 struts 变量并期望它能够工作。请记住,${...} 内容会在页面的 HTML 发送到浏览器之前进行处理。当 JavaScript 在浏览器中呈现时,您只剩下文本表示形式。你需要做的是类似的事情(检查语法,我还没有使用过这个东西):

function initialize() {
    var title = "";
    <c:foreach var="marker" list="${markers}">
         title = title + "${marker.street}";
    </c:foreach>
}

无论如何,沿着这些思路......基本上你的浏览器看到的Javascript看起来像

function initialize() {
    var title = "";
    title = title + "Street1";
    title = title + "Street2";
    title = title + "Street3";
    title = title + "Street4";
}

我希望这是有道理的,并且是与你所问的内容相关。
顺便说一句,通常有更好的方法来完成此功能,例如构建动态 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):

function initialize() {
    var title = "";
    <c:foreach var="marker" list="${markers}">
         title = title + "${marker.street}";
    </c:foreach>
}

Something along those lines anyway... Basically the Javascript seen by your browser will look like

function initialize() {
    var title = "";
    title = title + "Street1";
    title = title + "Street2";
    title = title + "Street3";
    title = title + "Street4";
}

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?

南…巷孤猫 2024-11-07 12:42:19

您必须在请求或会话中设置该变量,然后使用 jsp 标记访问它,如下所示,

var myVar= '<c:out value="${requestScope.myVar}"/>';

然后在 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 so

var myVar= '<c:out value="${requestScope.myVar}"/>';

then 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

乱了心跳 2024-11-07 12:42:19

您可以在服务器上将对象转换为 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.

┈┾☆殇 2024-11-07 12:42:19

你可以尝试像这样访问它。
但是你必须使用循环来从列表中获取每个对象,将其放在值堆栈上,然后获取它的每个对象。否则你可以要求 ognl 为你做这件事。就像

<script type="text/javascript">

function initialize() 
{
    var titel = "";
    for(var i, "${markers}")
    {
        titel = titel+ <s:property value="%{markers.get(i).street}">;
    }
}

尝试一下,因为 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

<script type="text/javascript">

function initialize() 
{
    var titel = "";
    for(var i, "${markers}")
    {
        titel = titel+ <s:property value="%{markers.get(i).street}">;
    }
}

just try it since OGNL has capacity to access the object on the value stack

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文