invoking a method on model with parameter

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

invoking a method on model with parameter

Bob
Hi

I am very new to thymeleaf (trying it out) so excuse me if this is a silly question.  I am using spring3 and pass an object, camelContext, to a view by adding it to the model.

I can access the properties of the object, but don't seem to be able to invoke a method on it.  So for example the following works fine except for the final th:text which objects to the syntax:

<tbody>
      <tr th:each="route,routeStat : ${camelContext.routes}">
        <td th:text="${routeStat.count}">1</td>
        <td th:text="${route.id}">route-1</td>
        <td th:text="${route}">Route  description</td>
        <td th:text="${camelContext.getRouteStatus( ${route.id} ) }">Started</td>
      </tr>
</tbody>
The exception is:
SpelParseException: EL1043E:(pos 30): Unexpected token.  Expected 'rparen())' but was 'lcurly({)'

Am I trying to do something which cannot be done, or is my syntax just off?

Is there another way?  I have seen elsewhere that it is apparently possible to access any service bean.  (The camelContext I am passing in the model is also a bean).  But I haven't found any example of invoking a method on a bean with a parameter like above.

Any suggestions appreciated.

Regards
Bob
Reply | Threaded
Open this post in threaded view
|

Re: invoking a method on model with parameter

Zemi
Administrator
Hello,

you don't need that extra ${...}. Try
   <td th:text="${camelContext.getRouteStatus(route.id)}">

${...} means "evaluate whatever it is inside".

Regards,
   Zemi

Bob
Reply | Threaded
Open this post in threaded view
|

Re: invoking a method on model with parameter

Bob
Perfect.  Thanks Zemi.
Reply | Threaded
Open this post in threaded view
|

Re: invoking a method on model with parameter

phdvictor
This post was updated on .
Hi guys.

I have an similar problem, look:

th:text="${tenantconf.getAtt(tenantid)}"

tenantid is a string value and it is not null, the code with spring:

@Configuration
public class TenantProperties {
    @Bean(name = "tenantconf")
    public Configuration hasFeature(String v) {
    Configuration conf = new Configuration(v);
        return conf;
    }

public class Configuration{
        String att;
       
        Configuration(String v){
                this.att = v;
        }
       
        public String getAtt(){
                return this.att +" hi";
        }
       
}

This return error, can you help me?

EL1011E:(pos 11): Method call: Attempted to call method getAtt(java.lang.String) on null context object

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

Re: invoking a method on model with parameter

Zemi
Administrator
Hi Victor,

I guess you haven't set your 'tenantid' bean into the model.

Try something like
  model.addAttribute("tenantid", tenantid);

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

Re: invoking a method on model with parameter

phdvictor
Thanks for your help, I am beginner in spring and thymeleaf...

I would like to do the same thing explained below, but sending a value to the method, it would be possible?

http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html

5. Spring beans

"${@urlService.getApplicationUrl()}"

@Configuration
    public class MyConfiguration {
        @Bean(name = "urlService")
        public UrlService urlService() {
            return new FixedUrlService("somedomain.com/myapp"); // some implementation
        }
    }
   
    public interface UrlService {
        String getApplicationUrl();
    }

Regards,
Victor