2010-06-02 4 views
2

message-property-code가 존재하지 않으면 분기하려고합니다.message.properties-code가 존재하는 경우 어떻게 분기합니까?

<g:if test="${message(code: 'default.code.foo')}"> 
    true 
</g:if><g:else> 
false 
</g:else> 

true를 대답해야하는 경우가 default.code.foofalse하지 않을 경우라는 메시지 속성입니다.

속성이 없으면 코드에 응답하기 때문에 실패합니다.

답변

7

당신은 빈 문자열의 기본을 제공 할 수 있습니까?

<g:if test="${message(code: 'default.code.foo', default:'')}"> 
    true 
</g:if><g:else> 
false 
</g:else> 

Groovy's truth

+0

영리를 위해 +1 –

+0

"$ {g.message (code : 'specific.code.foo', 기본값 : ''} ?: g.message (code : 'default.code .foo '} " 을 사용할 수 있습니다. –

2

그런 다음 반환 된 문자열이 코드 자체인지 테스트 해보십시오.

<g:if test="${message(code: 'default.code.foo') == 'default.code.foo'}"> 
    true 
</g:if><g:else> 
false 
</g:else> 

여러 곳에서이 작업을 수행하고 코드를 좀 더 간결하게 만들려면이 로직을 태그 lib에 넣으십시오.

class MsgTagLib { 
    static namespace = 'msg' 

    def messageSource 

    private static final NO_ARGS = [].toArray(); 

    def exists = {attrs -> 

     try { 
      messageSource.getMessage(attrs.code, NO_ARGS, Locale.default) 
      out << true 
     } catch (NoSuchMessageException e) { 
      out << false     
     } 
    } 
} 

그런 다음 사용하여 GSP에서 이것을 호출 할 수 있습니다 태그 LIB 위 100 % 안된

  1. <msg:exists code="default.code.foo"/> 
    

    주의, 조심!

  2. 그것의 형태로 exists 태그 위에 지원하지 않는 매개 변수 메시지
+0

에서 false로 동일시한다 때때로 그것은 솔루션 ... :) 있는지 그게 해결을 쉽게 볼을 멀리! – skurt

관련 문제