Custom Expression Utility Objects

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

Custom Expression Utility Objects

dostermeier
Hi,

Is there a way for me to register my own Expression Utility Object such as #dates or #messages ?

I have situations where I want to apply custom transformations to some model objects and at the moment I am writing custom AttrProcessors which seems to limit how I can use the transformed data.

ie: now I have

<div ... namespace:formatExecutionTime="${executionTime}"/>

where the formatting of my execution time is a custom utility

I would prefer to be able to say

<div th:text="#myUtil.formatExecutionTime(executionTime)"/>

Any ideas welcome.

Cheers,
-Daniel

Reply | Threaded
Open this post in threaded view
|

Re: Custom Expression Utility Objects

Zemi
Administrator
Hello,

please take a look at the following post with a very similar question:
  http://forum.thymeleaf.org/extend-language-td3454074.html

Reply | Threaded
Open this post in threaded view
|

Re: Custom Expression Utility Objects

danielfernandez
Administrator
Hi,

As is said in that post, your quickest option seems to be simply creating your utility object and adding it to the context when executing your templates. Have in mind that, doing that, you will not have to use the "#" symbol before the object name.


variables.put("myUtil", myUtilityObject);
WebContext ctx = new WebContext(request. variables);

<div th:text="myUtil.formatExecutionTime(executionTime)"/>


Regards,
Daniel
Reply | Threaded
Open this post in threaded view
|

Re: Custom Expression Utility Objects

dostermeier
Thanks for the response, that makes sense.  Apologies for the duplicate question.

Cheers,
-Daniel
Reply | Threaded
Open this post in threaded view
|

Re: Custom Expression Utility Objects

samsepiol
In reply to this post by danielfernandez
hi, sorry but Im new here, and i can't find where does this code should be...
where from i get the "variables" and "request" objects?

I have mi @Controller
and I my method where i redirect to my main screen


    @RequestMapping(value = "/")
    public String searchNavigate(Model model) throws Exception
    {
        String searchForm="A";
        model.addAttribute("searchForm", searchForm);
        return "colllock/search";
    }