无法访问FTL文件中从Modelmap发送的对象列表 [英] cannot access list of object sent from modelmap in FTL file
问题描述
我是ftl(FreeMarker模板)的新手.我使用这样的模型映射从控制器发送对象列表
Im new to ftl(FreeMarker template). I send a List of object from my controller using model map like this
@RequestMapping(value="/xxx")
public String myMethod(ModelMap model){
List<MyDTO> dto = mymethods(); // I return some list objects in this method
model.addAttribute("dto",dto);
model.addAttribute("dtoLength",dto.size());
return "myview";
}
MyDTO.java
public class myDTO{
Public String var1;
Public String var2;
public String getVar1() {
return var1;
}
public void setVar1(String var1) {
this.var1 = var1;
}
public String getVar2() {
return var2;
}
public void setVar2(String var2) {
this.var2 = var2;
}
}
现在我尝试从我的ftl的javascript访问dto.我尝试了很多这样的方法:
Now i try to access dto from my ftl's javascript.I tried it many ways like this:
var dto=${dto};
它说$ {....}中必须是字符串,整数,布尔值..等等等等
it said whatever must be inside ${....} should be either string,integer,boolean ..blah blah
所以我想到了这样迭代以获得我想要的东西
So i thought of iterating it like this to get what i want
var valueIWanted;
if(${dtoLength}>0){
for(var i=0 ; i < ${dtoLength} ; i++){
ticketList[i] = ${ticket[i].var2};
}
}
但是我也无法通过这种方式访问它.我应该在这里做什么?我想我缺少语法.有人可以帮我吗?
but i was not able to access it this way too.what am i supposed to do in here ?I think i m missing the syntax.Can anyone help me out with this?
推荐答案
您可以通过以下方式轻松访问modelMap数据:
You can easily access modelMap data as :
<#list dto as dtoList>
<tr>
<td>${dtoList.var1}</td> <td>${dtoList.var1}</td>
</tr>
</#list>
您不需要发送长度DTO
,如果您将其用于其他目的,则可以使用它.
You don't need to send the length of DTO
, if you are using it for other purpose then you can use it.
,您可以简单地迭代列表,不需要for
循环.
and you can simply iterate list, you don't need for
loop.
查看此.
如果要使用JavaScript访问数据,则可以在控制器页面加载时使用ajax请求,并向您发送包含数据的响应,然后处理该数据并在FTL页面上打印.如果可能的话,请使用一些JavaScript框架,以便轻松便捷地处理此类请求.
If you want to access the data using the JavaScript then you can use ajax request on page load on controller and it sends you a response with data and then process that data and print on FTL page. If possible then use some JavaScript framework so to handle such a request become easy and convenient.
仍然可以执行:var dtoList= "${dto}";
.不是没有报价.
Still you can do : var dtoList= "${dto}";
. not without quote.
请参见 SO答案.
用于检查是否为空:
[#if dto??]
//inside it write all your code
[/#if]
您可以在此处进行检查.
带有示例.
这篇关于无法访问FTL文件中从Modelmap发送的对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!