从Javascript/JQuery动态访问Java模型列表 [英] Access to Java model List from Javascript/JQuery dynamically

查看:72
本文介绍了从Javascript/JQuery动态访问Java模型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个List<Collaborateur>模型,它从我的Controller Action到我的JSP.而且我在JQuery/Javascript中具有以下功能:

I have a model as List<Collaborateur> coming from my Controller Action to my JSP. And I have the following function in JQuery/Javascript:

function showCollaborateurEmail(index) {
  alert("${collaborateurs[1].email}"); // Works
  alert("${collaborateurs[2].email}"); // Works
  //....
  alert("${collaborateurs[index].email}"); // Not working
}

所以我想通过调用函数showCollaborateurEmail(index)动态显示我的Collaborateur模型的电子邮件.

So I want to show the email of my Collaborateur model dynamically by calling the function showCollaborateurEmail(index).

谢谢您的帮助!

推荐答案

您面临的问题是服务器端代码与客户端代码之间的数据交换.有多种方法可以实现您的目标.您可以...

The problem that you face is the exchange of data between code on server side and code on client side. There are multiple ways to achieve your goal. You can...

  1. 将列表元素存储在隐藏的表单域中(转义定界符可能是一个挑战)或
  2. 生成javascript-JSP中服务器端的代码,用于使用list-elements或
  3. 创建javascript数组
  4. 使用ajax.
  1. store the list-elements in a hidden form-field (escaping of delimiter might be a challenge) or
  2. generate javascript - code on server side in JSP that creates a javascript array with the list-elements or
  3. use ajax.

1和2是临时的-解决一个小问题的方法. 3是一个更大的解决方案(取决于您的技术环境),它还提供了避免页面重新加载的可能性.

1 and 2 are rather ad hoc - solutions for a small problem. 3 is rather a bigger solution (depending on your technical environment) which additionally provides the possibility to avoid page reloads.

这篇关于从Javascript/JQuery动态访问Java模型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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