从Grails中的GSP访问类字段

从Grails中的GSP访问类字段,grails,gsp,Grails,Gsp,如何从Grails中的GSP访问类的字段?例如: geocoder.geocode({ 'address': 'london' }, geocoder.geocode({ 'address': ${search.city} }, 我需要在用户插入地址后以编程方式获取地址。例如: geocoder.geocode({ 'address': 'london' }, geocoder.geocode({ 'address':

如何从Grails中的GSP访问类的字段?例如:

geocoder.geocode({
      'address': 'london'
   }, 
geocoder.geocode({
      'address': ${search.city}
   }, 
我需要在用户插入地址后以编程方式获取地址。例如:

geocoder.geocode({
      'address': 'london'
   }, 
geocoder.geocode({
      'address': ${search.city}
   }, 
其中search是类,city是字段。 有办法做到这一点吗? 谢谢

更新

我试过这个: 在控制器中:

def map = {
    def searchInstance = Search.get(1)
    [locationList : Location.list(), search:searchInstance]
}
他认为:

function initialize() {
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({
      'address': ${search.city}
   }, 
   function(results, status) {
      if(status == google.maps.GeocoderStatus.OK) {

          var myMapOptions = {
                  zoom: 8,
                  center: results[0].geometry.location,
                  mapTypeId: google.maps.MapTypeId.ROADMAP
                };

                map = new google.maps.Map(document.getElementById("map_canvas"),
                    myMapOptions);

                <g:each in="${locationList}" status="i" var="location">     
                var point${location.id} = new google.maps.LatLng(${location.lat}, ${location.lng});
                var myMarkerOptions${location.id} = {
                      position: point${location.id}, 
                      map: map
                     };
                if(map.getCenter().distanceFrom(point${location.id}) < 500000)
                    var marker${location.id} = new google.maps.Marker(myMarkerOptions${location.id});   
            </g:each>

      }
   });
函数初始化(){
var geocoder=new google.maps.geocoder();
地理编码({
'地址:${search.city}
}, 
功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
var myMapOptions={
缩放:8,
中心:结果[0].geometry.location,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=new google.maps.map(document.getElementById(“map_canvas”),
myMapOptions);
var point${location.id}=new google.maps.LatLng(${location.lat},${location.lng});
var myMarkerOptions${location.id}={
位置:点${location.id},
地图:地图
};
if(map.getCenter().distanceFrom(point${location.id})<500000)
var marker${location.id}=new google.maps.marker(myMarkerOptions${location.id});
}
});
}

我可以从视图中访问map闭包返回的位置列表,但无法访问搜索实例。
有什么想法吗?

您不能将
搜索变量作为返回吗?

如果您将该类的实例作为模型从控制器传递到视图,您可以像平常一样访问字段

Controller: searchController {
    def search = {
        def searchInstance = Search.get(1) //assuming this gets the search that you want
        [search:searchInstance] // return searchInstance to the view under the alias search
    }
}

Gsp: search.gsp {
    geocoder.geocode({
      'address': ${search.city}
   },
}

geocoder.geocode({…})
部分已经在GSP中的
${}
中了吗?不,它在javascript脚本中,但不在${}中。您的解决方案看起来正是我需要的,但它对我不起作用。可能是因为我需要城市字段,不是在search.gsp中,而是在map.gsp中。我在控制器中还有一个名为map的附加闭包,我用它来调用map.gsp。它给出了一个异常:无法获取null objectIt上的属性“city”,毕竟它没有给出任何错误。但这种观点从未出现过。请查看问题的更新以检查代码。我发现了错误。我必须在引号内调用搜索实例,如下所示:'地址':“${search.city}”