Creating URL

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

Creating URL

niels
Hi,

in Play 2 there are routings compliersafe, so I can get a reverse-routing like routes.myController.myAction(params). I tried something similar with Spring MVC by defining a class URL which contains all URLs. Furthermore I create a method URL.filledURL(String, Object...), which takes the URL and filled the parameters, in this case as part of the URL, for example /show/{tagId}; where tagId is the parameter. My method assumes that the parameters has the same orders in the array like in the text. For complex situation I have a method which gets a map, where I can use named parameters.
I saw in Thymeleaf I can use @{'/details/'(orderId=${o.id})}. So here my questions:

1. How can I replace 'details' with a string which is defined as a Java-Constant (if possible compiler-safe)?

2. If the constant contains variables like '/details/{orderId}' will the orderId replaced with the value of o.id? Or can I archive this easily with an extension?

Hope the question is clear enough and not to stupid (I'm a newbie).
Niels
Reply | Threaded
Open this post in threaded view
|

Re: Creating URL

Zemi
Administrator
Maybe the preprocessing syntax could help you. Take a look at
   http://forum.thymeleaf.org/Create-restful-URL-with-th-href-td4024984.html
Reply | Threaded
Open this post in threaded view
|

Re: Creating URL

niels
Well with this I can create a RESTful URL, but this wasn't the point. The point was to reuse the existing JAVA-Constants which has values which containts the variables as described.

Niels
Reply | Threaded
Open this post in threaded view
|

Re: Creating URL

niels
I try to make my more direct.
1. Is it possible to access Java-Code specially Java-Constants from a template? I didn't found an example so I guess that's not possible.

2. Can I change the behavior of the @-syntax, so that I can replace named parameters, For example @{/details/{orderId}(orderId=${o.id}, otherParam=12)} should end in /detail/1?otherParam=12 (if we assume that o.id is 1) ?

Hope that the question now is more clear.

Niels
Reply | Threaded
Open this post in threaded view
|

Re: Creating URL

Emanuel
Administrator
1. It might be possible.  I've accessed non-bean class members in my templates by just giving the full name of the field/method.  OGNL (the expression language which Thymeleaf uses), if it can't find a Java bean property for the name that you gave, tries to find a field with that name, eg:

<div th:text="${myJavaObject.CONSTANT_NAME}"></div>
Reply | Threaded
Open this post in threaded view
|

Re: Creating URL

niels
Thanks Emanuel,

of course that's a good idea! In spring mvc I have access to all beans via ${beans.url.CONSTANT}. This should work. Unusual access but's a solution. Unfortunately without refactoring, code completion or compiler-safety-support, but I fear this is the price of natural templates.

Can anyone add some information about the second question? Or where is the point to discuss such feature request?

Niels
Reply | Threaded
Open this post in threaded view
|

Re: Creating URL

danielfernandez
Administrator
That second syntax you talk about (what Spring MVC would call "path variables") is not currently available in thymeleaf. You would need to append literals and variables, like:

@{'some/path/' + ${var1} + '/' + ${var2}}

You can add a feature request at https://github.com/thymeleaf/thymeleaf/issues if you want to track this feature.

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

Re: Creating URL

niels
I put it to https://github.com/thymeleaf/thymeleaf/issues/89 which should get the tag enhancement.