使用 Play! 将 json 对象属性注入锚点的 href 属性中框架

发布于 2024-11-16 00:07:56 字数 1121 浏览 0 评论 0原文

我有一个 jQuery ajax 调用,如下所示:

    $.ajax({
       type     : 'GET',
       dataType : 'json',
       url      : '/Zoo/animals',
       success  : function(data){
           var count = data.length;               
           for(var i = 0; i < count; i++) {
               ${'#tableBody').append('<tr><td>');
               ${'#tableBody').append('<a href="@{Animal.index(data[i].name)}">' + data[i].name + '</a>');
               ${'#tableBody').append('</td></tr>');
           }
       }
    })

现在,除了 href 属性之外,所有这些都可以正常工作。玩!给我以下错误:

Exception raised was NullPointerException : Cannot get property 'name' on null object.

从 ajax 调用返回的 json 字符串很好,因为链接的文本按预期显示为动物的名称。我想将动物的名称传递给 Animal.index 操作,但无法这样做。

更新:

正如其他人提到的,我也尝试过这个:

${'#tableBody').append('<a href="@{Animal.index(' + data[i].name + ')}">' + data[i].name + '</a>');

这只会产生以下链接:

localhost:9000/animal/index?name=+%2B+data[i].name+%2B+

I have a jQuery ajax call that looks like this:

    $.ajax({
       type     : 'GET',
       dataType : 'json',
       url      : '/Zoo/animals',
       success  : function(data){
           var count = data.length;               
           for(var i = 0; i < count; i++) {
               ${'#tableBody').append('<tr><td>');
               ${'#tableBody').append('<a href="@{Animal.index(data[i].name)}">' + data[i].name + '</a>');
               ${'#tableBody').append('</td></tr>');
           }
       }
    })

Now all of this works fine except for the href attribute. Play! gives me the following error:

Exception raised was NullPointerException : Cannot get property 'name' on null object.

The json string that gets returned from the ajax call is fine as the text of the link shows up as the name of the animal as expected. I would like to pass the name of the animal to the Animal.index action however have been unable to do so.

Update:

As others have mentioned, I have also tried this:

${'#tableBody').append('<a href="@{Animal.index(' + data[i].name + ')}">' + data[i].name + '</a>');

which just results in the link of:

localhost:9000/animal/index?name=+%2B+data[i].name+%2B+

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

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

发布评论

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

评论(5

扭转时空 2024-11-23 00:07:56

问题是玩!在提供文件时(在执行任何 javascript 之前)生成 url。对这里或那里的字符串或引号进行任何更改都不会对您有帮助。

解决方法:

var url = "@{Animal.index(XXX)}".replace('XXX', data[i].name);
${'#tableBody').append('<a href="'+url+'">' + data[i].name + '</a>');

这允许播放!在“编译”时生成其 url,然后 javascript 在“运行”时简单地执行替换。不确定是否可以玩!将查找名为 XXX 的变量,如果是,则在服务器端添加一个名为 XXX 且值为“XXX”的哑变量。

更新

我没有玩!我前面的实例可以摆弄,但除了变量之外,您还可以通过引用 XXX: 直接将字符串作为值插入:

var url = '@{Animal.index("XXX")}'.replace('XXX', data[i].name);

然后您就不需要名为 XXX 的变量。尽管想法是相同的,但细节需要一些尝试。

The problem is that Play! generates the url when the file is served, well before any javascript is executed. No change to the string or quotes here or there is going to help you.

A workaround:

var url = "@{Animal.index(XXX)}".replace('XXX', data[i].name);
${'#tableBody').append('<a href="'+url+'">' + data[i].name + '</a>');

This allows Play! to generate its url at "compile" time, and then javascript simply executes a replace at "run" time. Not sure if Play! will look for a variable named XXX, if so, add a dumby variable named XXX with value "XXX" on the server side.

UPDATE

I don't have a Play! instance in front of me to fiddle with, but instead of a variable, you could probably also insert the string directly as a value by quoting the XXX:

var url = '@{Animal.index("XXX")}'.replace('XXX', data[i].name);

And then you wouldn't need a variable named XXX. The details require a bit of playing around, although the idea is the same.

酒与心事 2024-11-23 00:07:56

这听起来像是 jsAction 标签的工作。它生成一个 javascript 函数,其工作原理与 davin 的答案相同。

您可以将 var AnimalAction = #{jsAction @Animals.index(':name') /}; 放在 ajax 调用之前的某个位置,然后将链接生成代码更改为:

${'#tableBody').append('<tr><td>');
${'#tableBody').append('<a href="' + animalAction({ name: data[i].name }) + '">' + data[i].name + '</a>');
${'#tableBody').append('</td></tr>');

您会注意到生成的函数接受一个对象作为参数,因此它可以支持任意数量的参数。

文档中没有明确说明的是标签 (:name) 必须与相应控制器操作中的参数名称相同 (public static void index(String name )),当然必须与 routes 文件中指定的方式相同 (GET /animals/{name} Animals.index)。

This sounds like a job for the jsAction tag. It generates a javascript function which works along the same lines as davin's answer.

You would put var animalAction = #{jsAction @Animals.index(':name') /}; somewhere before your ajax call, then change your link generation code to:

${'#tableBody').append('<tr><td>');
${'#tableBody').append('<a href="' + animalAction({ name: data[i].name }) + '">' + data[i].name + '</a>');
${'#tableBody').append('</td></tr>');

You'll notice the generated function takes an object as it's argument, so it can support as many parameters as you want.

What isn't explicitly clear in the docs is that the label (:name) must be the same as the name of the parameter in the corresponding controller action (public static void index(String name)), which of course must be the same as how it's specified in the routes file (GET /animals/{name} Animals.index).

携余温的黄昏 2024-11-23 00:07:56

data 是一个 javascript 对象,您需要指定类似于打印链接文本的方式。你能试试这个吗?

 ${'#tableBody').append('<a href="@{Animal.index(' + data.name + ')}">' + data.name + '</a>');

data is a javascript object, you need to specify that similar to how you are printing the text of the link. Can you try this?

 ${'#tableBody').append('<a href="@{Animal.index(' + data.name + ')}">' + data.name + '</a>');
空心↖ 2024-11-23 00:07:56

您正在循环 data 数组,但需要通过索引 i 引用每个数据对象,就像 data[i] 一样。我会尝试这个:

$.each(data, function(k, item) {
    $('#tableBody').append('<tr><td><a href="@{Animal.index(' + item.name + ')}">' + item.name + '</a></td></tr>');
});

这将替换成功回调中的所有内容

You are looping over data array but you need to reference each data object by the index i so like data[i]. I would try this:

$.each(data, function(k, item) {
    $('#tableBody').append('<tr><td><a href="@{Animal.index(' + item.name + ')}">' + item.name + '</a></td></tr>');
});

This will replace everything inside your success callback

度的依靠╰つ 2024-11-23 00:07:56

你应该改变这一行 -

${'#tableBody').append('<a href="@{Animal.index(data[i].name)}">' + data[i].name + '</a>');   

到这个 -

${'#tableBody').append('<a href="@{Animal.index('+data[i].name+')}">' + data[i].name + '</a>'); 

you should change this line-

${'#tableBody').append('<a href="@{Animal.index(data[i].name)}">' + data[i].name + '</a>');   

to this-

${'#tableBody').append('<a href="@{Animal.index('+data[i].name+')}">' + data[i].name + '</a>'); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文