【问题标题】:Accessing Map values in gsp page Grails using variables as key使用变量作为键访问 gsp 页面 Grails 中的映射值
【发布时间】:2016-03-03 18:58:47
【问题描述】:

我正在尝试使用一个模板动态创建多个 div,该模板被调用了多次,每次我创建一个新的 div 时...下面的代码似乎工作正常。我将地图 tempMap 从控制器传递到以下格式的 gsp 页面

         tempMap = [key_1:v1,key_2:v2,key_3:v3] //from the controller

         //this is the gsp part
        <g:set var="counter" value="${1}" />
        <g:while test="${counter <= tempMap.counter}">
        <g:render template="travelDetailsToShow" />
        <g:set var="counter" value="${counter + 1}" />
        </g:while>

我需要根据也可以工作的计数器设置元素的 ID ...但我无法设置字段的值。

                <div class="col-sm-6" id="key_${counter}"> // This sets the id to key_1, key_2 depending on the counter value
                    <div class="form-group">
                        <label>Departure Date</label>
                        <span class="input-icon-right input-group">
                            <g:set var="temp" value="kep_${counter}" />
                            **<input type="text" name="key_${counter}" class="form-control" value="${tempMap?.key_${counter}}" readonly></input> // this does not work** 
                            <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
                        </span>
                    </div>
                </div>

我搜索了很多,但没有运气......谁能告诉我我哪里出了问题......感谢任何帮助......在此先感谢人们

【问题讨论】:

    标签: grails gsp


    【解决方案1】:

    渲染标签有一个模型属性。它的工作方式类似于从控制器传递到视图的模型。您可以使用它将数据传递到模板中。

    查看

    <g:each in="${1..tempMap.counter}" var="counter">
        <g:set var="key" value="key_${counter}">
        <g:render template="travelDetailsToShow" model="${[id: key, value: tempMap[key]]}"/>
    </g:each>
    

    模板

    <div class="col-sm-6" id="${id}">
        <div class="form-group">
            <label>Departure Date</label>
            <span class="input-icon-right input-group">
            <input type="text" name="${id}" class="form-control" value="${value}" readonly></input>
             <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
                            </span>
         </div>
     </div>
    

    【讨论】:

      【解决方案2】:

      感谢伊曼纽尔的回答......但我认为我不够清楚并误导你认为“关键”是静态的......因为你的逻辑会期望静态值“关键”......地图的正确结构如下...

             tempMap = [key_1:v1,anotherkey_1:v2,yetanotherkey_1:v3,key_2:v4,anotherkey_2:v5,yetanotherkey_2:v6]
      

      我还没有尝试过你发布的解决方案,但有另一个解决方案可以工作......下面的代码......

                      <div class="col-sm-6" id="key_${counter}">
                          <div class="form-group">
                              <label>Departure Date</label>
                              <span class="input-icon-right input-group">
                                  <input type="text" name="key_${counter}" class="form-control" value='${tempMap?."${'key_' + counter}"}' readonly></input>
                                  <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
                              </span>
                          </div>
                      </div>
                      <div class="col-sm-6" id="anotherkey_${counter}">
                          <div class="form-group">
                              <label>Return Date</label>
                              <span class="input-icon-right input-group">
                                  <input type="text" name="anotherkey_${counter}" class="form-control" value='${tempMap?."${'another_' + counter}"}' readonly></input>
                                  <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
                              </span>
                          </div>
                      </div>
                  </div>
      

      问题是我没有正确引用 Map 中的值...失败的代码如下所述。

                                      <div class="col-sm-6" id="departureDate_${counter}">
                          <div class="form-group">
                              <label>Departure Date</label>
                              <span class="input-icon-right input-group">
                                  **<input type="text" name="departureDate_${counter}" class="form-control" value="${tempMap?.${departureDate_counter}"} readonly></input>** // THIS CODE DOES NOT WORK. This will look for a key ${departureDate_counter} and will through a gsp error that tags have not been closed
                                  <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
                              </span>
                          </div>
                      </div>
      

      虽然我已经找到了解决我的问题的方法,但如果我们发布更多解决方案会很好,因为我觉得网络上并没有很多针对 grails 和 gsp 的解决方案......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-26
        • 2012-01-23
        • 1970-01-01
        • 2014-09-06
        • 2023-03-09
        • 2022-01-22
        相关资源
        最近更新 更多