2015-12-21 112 views
0

我想弄清楚如果我有一個數組,有多個字符串與它關聯或者它只是一個字符數組(字符串)。現在,如果我輸入1234的單數ID,我將得到一個1,2,3,4的數組,但如果我說有兩個ID的12和34,數組將返回12,34。如何檢查字符串時它應該始終是一個字符串數組?確定字符串數組或字符串數​​組Grails

<div class="area"> 
<h2>Select all people who will be Traveling</h2> 
<div> 
<g:if test="${disabled=='false'}"> 
<g:select name="selector" class="claim" value="None" from="${fullList}" optionKey="studentID" optionValue="${{it.firstName + ' ' + it.lastName}}" noSelection="${['null':' ']}" disabled="${disabled}"/> 
    <g:if test="${tripInstance?.student!= null }"> 
    <g:each var="i" in="${(0..<tripInstance?.student?.length) }"> 
    <div> 
    <input class='field' name='Name' readonly type='text' value='${fullList.firstName[(tripInstance?.student[i]).toInteger()]} ${fullList.lastName[(tripInstance?.student[i]).toInteger()]}'/> 
    <input class='field' name='student' readonly type='hidden' value='${tripInstance?.student[i]}'/> 
     <label class='removeEdit fakeLink'> Remove</label> 
    </div> 
    </g:each> 
    </g:if> 
</g:if> 

<g:if test="${disabled=='true'}"> 
<g:if test="${tripInstance?.student!= null }"> 
<g:each var="i" in="${(0..<tripInstance?.student?.size()) }"> 
    <div> 
    <input class='field' name='student' readonly disabled="${disabled}" type='text' value='${tripInstance?.student[i]}'/> 
    </div> 
</g:each> 
</g:if> 
</g:if> 
</div> 
</div> 

我試圖根據類來檢查。我不能根據大小來檢查,因爲字符串將具有大小,並且字符串數組也是如此。它是一個字符串數組,而不是Int數組,因爲其他部分代碼需要這種格式。希望我不會忽略一些簡單的東西。

+0

它不是'1,234'還是'123,4'或'1,2,34'或'12,3,4'?基本上,你如何確定有效的狀態? –

+0

它可能是ID爲1和234的學生,但數組會註冊那些是兩個單獨的元素,但是如果只有一個學生ID爲91或123甚至4(任何數字但只有一個入口)陣列將成爲一個字符數組,並將其分割爲多個元素 – tmurphy

+0

是的,但是您如何消除歧義?你說「數組」...什麼數組? –

回答

2

Grails在params對象上提供了一種方便的方法來始終返回一個List,而不是可能遍歷一個String。它方便地稱爲list()

def ids = params.list('ids') 

它可以在文檔中Simple Type Converters下找到。

+0

謝謝!那樣做了! – tmurphy