service method invocation

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

service method invocation

hammadafridi
Hello there,
I am new to thymeleaf and exploring if it is better to use it in the product i have been working on. At the moment I am using freemarker. The main question I have is that in freemarker template I use different application service that can used in my templating system by the user to get data from the db etc.

Freemarker provides TemplateModelMap that holds all the services that can be used in freemarker templates. I need to know is it possible to hook the services into thymeleaf templates so that I can call method directly from the template? If there is how ? If not what are the alternatives. Do you have any examples for doing this.


Your advice will be much appreciated.Thanks

Regards,
Hammad.
Reply | Threaded
Open this post in threaded view
|

Re: service method invocation

Emanuel
Administrator
I don't know Freemarker that well, so apologies if I've misunderstood what you're trying to do.  What I read was 'can I call methods directly from the template', and I can think of 2 ways to do this:

1) Anything you add to the Spring model can be accessed from Thymeleaf, and using SpEL you can invoke methods on objects in the model, eg: ${modelObject.doSomeMethod(someParameters)}

2) You can create utility classes in a custom dialect which can be added to the Thymeleaf processing context (ie: they can be made available to all your templates) and can then do pretty much anything.  They're called 'expression objects' in Thymeleaf, and they sound similar to those services in the Freemarker TemplateModelMap you mentioned.

For examples of creating a custom dialect, see the Extending Thymeleaf doc (http://www.thymeleaf.org/doc/html/Extending-Thymeleaf.html), then for an example of creating an expression object to add to the dialect, I did something in a presentation recently with this code:

 - dialect class: https://github.com/ultraq/thymeleaf-presentation/blob/master/Java/org/thymeleaf/presentation/dialect/PresentationDialect.java
 - the expression object I added to the dialect: https://github.com/ultraq/thymeleaf-presentation/blob/master/Java/org/thymeleaf/presentation/dialect/JodaUtility.java

I think the main thing to take away from the example is that the dialect implements the IExpressionEnhancingDialect interface, which adds a method that returns the expression objects that you want to add to the processing context.  Expression objects are then accessed like the standard Thymeleaf ones - with the # before the object name, eg: http://www.thymeleaf.org/doc/html/Using-Thymeleaf.html#appendix-expression-utility-objects

Reply | Threaded
Open this post in threaded view
|

Re: service method invocation

Hammad Afridi
Thanks so much for the reply and apologies for not getting back to you earlier as I was away for holidays.

I liked the second approach that seem to be much more cleaner than the first. Would you be able to refer me to the template where you have used this?

I will try this out and see if that fits my requirements or not.


Thanks,
Hammad.
Reply | Threaded
Open this post in threaded view
|

Re: service method invocation

Yawar J S
In reply to this post by Emanuel
How to use spring model classes in thymeleaf Template ?? I have tried the first approach provided by @Emanuel in previous answer. I used below code

<div th:object="${templateHelpingClass.saySomething()}">
    <p>
       I am default text
  </p>
</div>

tried with different classes and model and helper classes. but template engine shows error that calling function on " null " . please provide some way to access my required classes in this template engine. There might be someway to find which or the classes are accessible in this templating code and how to add further classes to be accessed from here.

Thanks,
Yawar J S
Reply | Threaded
Open this post in threaded view
|

Re: service method invocation

Emanuel
Administrator
How are you adding your templateHelpingClass onto your Spring model?  2 ways that I know how are:

1. Using the @ModelAttribute annotation on a method in the same controller that processes the request for the view you're going to use it in:

@ModelAttribute("templateHelpingClass")
public HelperClass addHelperClass() {
  return new HelperClass();
}


2. Adding it to the model parameter in your request method, before processing the view you want to use it in:

@RequestMapping(...)
public String myMethod(Model model) {
  model.add("templateHelpingClass", new HelperClass());
  return "myview.html";
}
Reply | Threaded
Open this post in threaded view
|

Re: service method invocation

hendisantika
Emanuel wrote
How are you adding your templateHelpingClass onto your Spring model?  2 ways that I know how are:

1. Using the @ModelAttribute annotation on a method in the same controller that processes the request for the view you're going to use it in:

@ModelAttribute("templateHelpingClass")
public HelperClass addHelperClass() {
  return new HelperClass();
}


2. Adding it to the model parameter in your request method, before processing the view you want to use it in:

@RequestMapping(...)
public String myMethod(Model model) {
  model.add("templateHelpingClass", new HelperClass());
  return "myview.html";
}
Thank You very much @Emanuel.

You save my work.

I almost give up to find the answer "How to call Java Method from Thymeleaf".