I have a vraptor4 project and i want to use apache velocity
as the template engine.
So i specialized the br.com.caelum.vraptor.view.DefaultPathResolver
as
@Specializes
public class VelocityPathResolver extends DefaultPathResolver {
@Inject
protected VelocityPathResolver(FormatResolver resolver) {
super(resolver);
}
protected String getPrefix() {
return "/WEB-INF/vm/";
}
protected String getExtension() {
return "vm";
}
}
That work fine, but I cannot have @Named
components in my templates.
Having
@SessionScoped
@Named("mycmp")
public class MyComponent implements Serializable {
private static final long serialVersionUID = 1L;
private String name = "My Component";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
I cannot refer to it as ${mycmp.name}
in my velocity template (.vm), but if i use .jsp it works fine.
To solve it i specialized the br.com.caelum.vraptor.core.DefaultResult
as
@Specializes
public class VelocityResult extends DefaultResult {
private final MyComponent mycmp;
@Inject
public VelocityResult(HttpServletRequest request, Container container, ExceptionMapper exceptions, TypeNameExtractor extractor,
Messages messages, MyComponent mycmp) {
super(request, container, exceptions, extractor, messages);
this.mycmp = mycmp;
}
@PostConstruct
public void init() {
include("mycmp", mycmp);
}
}
Is there a better approach for having @Named
components in velocity templates?