Answering my own question... and boy does this seem complicated to me! So if
there is an easier way then described below _please_ let me know.
The "method class" I came up with to solve the problem:
public class UserForUsernameMethod implements TemplateMethodModel {
import freemarker.ext.beans.BeanModel;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import info.magnolia.cms.security.SecuritySupport;
import info.magnolia.cms.security.User;
import info.magnolia.cms.security.UserManager;
import java.util.List;
public class UserForUsernameMethod implements TemplateMethodModel {
public TemplateModel exec(List args) throws TemplateModelException {
if (args.size() < 1) {
throw new TemplateModelException("Wrong arguments");
} else {
String userManagerName = "admin";
String userName = (String) args.get(0);
if (args.size() > 1) {
userManagerName = (String) args.get(1);
}
User user = magnoliaUserForUsernameAndManagerName(userName,
userManagerName);
if (user != null) {
return new BeanModel(user,
DefaultObjectWrapper.getDefaultInstance());
} else {
return TemplateModel.NOTHING;
}
}
}
private User magnoliaUserForUsernameAndManagerName(String userName, String
userManagerName) {
UserManager userManager =
SecuritySupport.Factory.getInstance().getUserManager(userManagerName);
return userManager.getUser(userName);
}
}
And to add the method to the model in the template class extending
TemplatedMVCHandler I had to overwrite the renderHtml() method and add the
method there:
@Override
public void renderHtml(String view) throws IOException {
// no rendering if view is null
if(StringUtils.isEmpty(view)){
return;
}
String template = this.getTemplateName(view);
if (template != null) {
Map data = new HashMap();
data.put("this", this);
data.put("view", view);
data.put("userForUsername", new UserForUsernameMethod());
PrintWriter writer;
try {
writer = getResponse().getWriter();
}
catch (IllegalStateException e) {
// getResponse().getOutputStream() has already been called
writer = new PrintWriter(getResponse().getOutputStream());
}
FreemarkerUtil.process(template, data, writer);
}
}
Afterwards, I was able to access the user data in my template:
test: ${userForUsername("test").password}
Quite a bit of work! At the moment, even custom JSP tags seem easier to me. But
maybe after getting used to this...
-will
On 03.12.2009, at 17:45, Will Scheidegger wrote:
> Dear Magnolians
>
> Can someone give me a quick primer on how to call java methods from a
> Freemarker template? From looking at the documentation one does not seem to
> be able to simply call a method in an object. It looks like the methods need
> to be implemented as subclass of "TemplateMethodModel" (see [1]). This seems
> quite complicated, but I guess I could do that. But then, where is this
> method added to the data model (i.e. "root.put("methodName", new
> MethodClass())?
>
> To be more specific: I would like to access the user info in a Freemarker
> template, where I only have the username. So I would need a method which
> would return the user (object? "data-model"? hash?) when passing in the
> username. I am using the TemplatedMVCHandler class to display the template.
> The template displays fine. But now I'm a bit stuck with adding the method...
>
> Thanks for the insight!
> -will
>
>
> [1] http://freemarker.sourceforge.net/docs/pgui_datamodel_method.html
>
> ----------------------------------------------------------------
> For list details see
> http://www.magnolia-cms.com/home/community/mailing-lists.html
> To unsubscribe, E-mail to: <[email protected]>
> ----------------------------------------------------------------
----------------------------------------------------------------
For list details see
http://www.magnolia-cms.com/home/community/mailing-lists.html
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------