2015-01-27 1 views
2

나는이 FTL에 다음 코드 :는`나누기

<#macro field label name value="" type="text"> 
    ${name} 
    ${name!"print if null"} 
    <div class="field"> 
     <div class="clearfix" id="${name}_field"> 
      <label for="${name}">${label}</label> 
      <div class="input"> 
      <input type="${type}" id="${name}" name="${name}" value="${value}"> 
       <span class="help-inline"></span> 
       <span class="help-block"></span> 
      </div> 
     </div> 
    </div> 
</#macro> 


<@field label="label" name="test" /> 

그리고는이 인쇄되어

foo-test 
test 
<div class="field"> 
    <div class="clearfix" id="foo-test_field"> 
     <label for="foo-test">label</label> 
     <div class="input"> 
     <input type="text" id="foo-test" name="foo-test" value=""> 
      <span class="help-inline"></span> 
      <span class="help-block"></span> 
     </div> 
    </div> 
</div> 

foo-test 내 응용 프로그램의 이름입니다 만 왜 거기에 인쇄되고 있는지 이해할 수 없다. foo-test을 검색하기 위해 Ctrl + F를 사용했다. ftl이나 컨트롤러에 아무 것도 없다.

이 외에도 name은 내 응용 프로그램의 이름을 가진 변수입니다. 그런데 왜 두 번째 인쇄가 매크로에 전달 된 올바른 값을 인쇄하나요 ??

<dependency> 
     <groupId>com.sparkjava</groupId> 
     <artifactId>spark-template-freemarker</artifactId> 
     <version>2.0.0</version> 
    </dependency> 

플러그인이 있습니다 : 이것은 ... 내가 메이븐을 사용하고 난이 종속성이 너무 촉발

정말 이상하다

<build> 
    <resources> 
     <resource> 
      <directory>src/main/resources</directory> 
      <filtering>true</filtering> 
     </resource> 
    </resources> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>exec-maven-plugin</artifactId> 
      <version>1.1</version> 
      <configuration> 
       <mainClass>com.example.foo.foo-test</mainClass> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

그리고 내 컨트롤러는 다음과 같습니다

..... 

    import spark.ModelAndView; 
    import spark.Spark; 
    import spark.template.freemarker.FreeMarkerEngine; 

    ...... 

    Spark.get("/foo", (request, response) -> { 
     Map<String, Object> attributes = new HashMap<>(); 
     return new ModelAndView(attributes, "test.ftl"); 
    }, new FreeMarkerEngine()); 
+0

Maven을 사용하고 있습니까? –

+0

@AleksandrM 예,이 문제와 관련이 있습니까? –

+0

maven에서 ftl-s를 어떻게 포장하고 있습니까? pom.xml을 보여줄 수 있습니까? –

답변

1

리소스를 필터링하도록 maven을 구성했기 때문에이 문제가 발생합니다. ${name} 자리 표시자를 프로젝트 이름으로 대체합니다.

pom에서 <resources>을 제거하거나 리소스 필터링이 필요한 경우 ftl-s 파일을 제외 할 수 있습니다.

<resources> 
    <resource> 
     <directory>src/main/resources</directory> 
     <filtering>true</filtering> 
    </resource> 
</resources> 

은 BTW maven-resources-plugin 문서에 대한 resource filtering 정확한 ${name} 자리에이 동작을 보여줍니다. :)

+1

그게 다야! 메이븐 (Maven)이 이런 일을했다는 것을 믿을 수 없다. 왜 매크로 내부에서만 일어 났는지 이해할 수 없다. 고마워! –

1

매우 이상합니다 ... 작동해야하며 나를 위해 작동합니다. FreeMarker가 구문 분석하기 전에 (예 : TemplateLoader -s가 그 작업을 수행 할 수 있음) 무언가가 해당 템플릿의 ${name}을 검색 및 대체한다고 의심합니다. 마찬가지로 ${name} 대신 ${name<#-- just a comment -->}을 쓰면 어떻게됩니까?

+0

당신 말이 맞아요. '$ {name <# - 그냥 코멘트 ->}'라고 쓰면 내가 원하는 것을 출력합니다. 그러나 첫 줄에'$ {name}'을 출력하면 이름이 정의되지 않았다는 오류가 발생합니다. 왜 매크로 내부에서만 발생합니까 ?? –

+0

FreeMarker가 가져 오기 전에 템플릿 파일에 어떤 마법을 적용했는지 모르겠다 ...'System.out.println (template)'을 할 수 있을까요?'template'은'fremarker.template.Template' 객체입니다. 템플릿 안에 무엇이 있는지 확인하십시오. 또한 FreeMarker 버전은 ('$ {. version}')입니까? 나는 10 년 전부터 일부 미리보기 릴리즈를 희망하지 않는다 ... – ddekany

+0

오히려,'System.out.println (template.getSource())'. – ddekany