GSP

2011-05-15 5 views
1

의 도메인 메소드를 호출 나는 <name> 또는 <adresse> 같은 사이의 문자열을 검색 할 수 있습니다 affichage(s)라는 내 도메인 클래스의 메소드 생성 : 나는 groovyConsole에이 기능을 실행 한GSP

enter code here 



def affichage(s){ 

def l=[] 
def regex = /\<.\w*.\>/ 
def matcher = s =~ regex 
matcher.each{match -> 
l.add(match)} 
l.each{ 
for (i in l){ 
println i 
}}}} 

을 그리고 OK입니다.

이 방법을 텍스트 필드에 적용하려면 어떻게해야합니까?

+0

무엇 반환 할 필요합니까 ... 같이 수 있을까? 'Matches '를 수집하는 이유는'Matches '가 필요하지 않지만'String' 만 믿으시겠습니까? 어쨌든 도메인 클래스의 인스턴스를 Model 파트로 전달하고 GSP에서 호출하십시오. 정확히 어떻게 - 텍스트 필드에서 무엇을해야하는지에 따라 GSP 코드 샘플이 도움이 될 것입니다. –

답변

5

grails 방식을 사용하려면 컨트롤러에서 도메인 객체를로드 한 다음보기에 전달합니다. 하여 컨트롤러에 다음과 같은 일이 : 당신이 재산을 받고 다음에

// assuming theDomainObject/anotherObject are loaded, preferably via service calls 
render view: 'theView', model: [object: theDomainObject, another: anotherObject] 

다음보기에서 당신은 먼저이 메소드를 호출하는에

${object.yourMethod()} 

${another.someprop} 

메모를 할 수 있습니다. 또한 중괄호 안에 컨트롤러에서 모델을 통과 한 것을 참조 할 수 있습니다.

${...} 

은 전달 된 모델 객체를 gsp에 지시합니다. grails는 그렇게 멋집니다.

0

이전 대답을 분명하게 만드십시오.

도메인 자체를 전달하지 마십시오. 모델의 인스턴스를 다음과 같이 전달합니다.

render(view: 'theView', model: [object: new MyDomain(), another: new YourDomain()] 

여기서 MyDomain 및 YourDomain은 Grails의 도메인 클래스입니다. 당신은 이런 식으로 뭔가가있는 GSP에서 텍스트 필드의 값을 채우려면 해당 태그를 호출 할 수 있습니다

// grails-app/taglib/com/demo/ParsingTagLib.groovy 
package com.demo 

class ParsingTagLib { 
    static defaultEncodeAs = 'html' 
    static namespace = 'parsing' 

    def retrieveContentsOfTag = { attrs -> 
     // The regex handling here is very 
     // crude and intentionally simplified. 
     // The question isn't about regex so 
     // this isn't the interesting part. 
     def matcher = attrs.stringToParse =~ /<(\w*)>/ 
     out << matcher[0][1] 
    } 
} 

당신은이 같은 태그 라이브러리를 만들 수 있습니다

0

... ...

<g:textField name="firstName" value="${parsing.retrieveContentsOfTag(stringToParse: firstName)}"/> 
<g:textField name="lastName" value="${parsing.retrieveContentsOfTag(stringToParse: lastName)}"/> 

이이 같은 컨트롤러에서 렌더링한다면 ... 생처럼 보이는 HTML 초래

// grails-app/controllers/com/demo/DemoController.groovy 
package com.demo 

class DemoController { 

    def index() { 
     // these values wouldn't have to be hardcoded here of course... 
     [firstName: '<Jeff>', lastName: '<Brown>'] 
    } 
} 

s ...

<input type="text" name="firstName" value="Jeff" id="firstName" /> 
<input type="text" name="lastName" value="Brown" id="lastName" /> 

나는 희망한다.

UPDATE :

당신이 정말로 무엇을하려고에 따라, 당신은 또한이 같은 것을 사용하여 태그 내에 전체 텍스트 필드 생성 일을 마무리에서 같을 수 있습니다 ...

// grails-app/taglib/com/demo/ParsingTagLib.groovy 
package com.demo 

class ParsingTagLib { 
    static namespace = 'parsing' 

    def mySpecialTextField = { attrs -> 
     // The regex handling here is very 
     // crude and intentionally simplified. 
     // The question isn't about regex so 
     // this isn't the interesting part. 
     def matcher = attrs.stringToParse =~ /<(\w*)>/ 
     def value = matcher[0][1] 
     out << g.textField(name: attrs.name, value: value) 
    } 
} 

그런 다음 GSP 코드는

<parsing:mySpecialTextField name="firstName" stringToParse="${firstName}"/> 
<parsing:mySpecialTextField name="lastName" stringToParse="${lastName}"/> 
관련 문제