从Grails中的GSP访问类字段



如何从Grails中的GSP访问类的字段?例如,以下内容:

geocoder.geocode({
      'address': 'london'
   }, 

用户插入地址后,我需要以编程方式获取地址。如下所示:

geocoder.geocode({
      'address': ${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>
      }
   });

}

我可以从视图访问地图关闭返回的位置列表,但不能访问搜索实例。有什么想法吗?

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

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

难道不能将search变量作为控制器中模型的一部分返回吗?

最新更新