: - :
입력 :> 출력에 "Hello World"출력은 다음과 같은 것이있다 그래서 customformat 기능을 수정하는 방법StringTemplate은 엄격한 모델 - 뷰 분리에 관한 것이기 때문에 이것이 불가능하다는 것을 알고 있습니다.
대신 형식이 지정된 문자열을 반환 한 컨트롤러에서 getter를 사용하는 것이 좋습니다.
당신은 도움이 질문에 찾을 수 있습니다
: 당신은 새를 만들 필요가
: 사실 embed java code inside a template
, 나는 형식화 된 문자열 게터에 대한 필요성을 피할 이렇게 간단한 방법을 발견 원하는대로 문자열의 서식을 지정할 수있는 StringRenderer.
public class MyStringRenderer extends StringRenderer
{
@Override
public String toString(Object o, String formatString, Locale locale) {
if (!("upperAndUnder".equals(formatString)))
return super.toString(o, formatString, locale);
// we want upper case words with underscores instead of spaces
return ((String) o).replaceAll(" ", "_").toUpperCase(locale);
}
}
그럼 당신은 템플릿 그룹이 새로운 렌더러에 대해 알려해야합니다 : 당신이 전에했던 것처럼
public static void main(String[] args) {
STGroup templates = new STGroupFile("test.stg");
templates.registerRenderer(String.class, new MyStringRenderer());
ST renderTemplate = templates.getInstanceOf("render");
renderTemplate.add("attributes", new String[]{"blahh blahh I'm a string", "I'm another string"});
System.out.println(renderTemplate.render());
}
는 그런 다음 형식으로 함수를 호출 할 수 있지만 매개 변수로 "upperAndUnder을"통과 :
group test;
delimiters "$","$"
render(attributes) ::= <<
<html>
$attributes:{ attribute | <div> $customFormat(attribute)$</div>}; separator="\n"$
</html>
>>
customFormat(name) ::= <<
$name; format="upperAndUnder"$
>>
인쇄하는 :
<html>
<div> BLAHH_BLAHH_I'M_A_STRING</div>
<div> I'M_ANOTHER_STRING</div>
</html>
참고 16,
:
Here's the original StringRenderer code
More info on Renderers