1

I have a problem when im try to call a method from view.

My Java class with methods

public class FuncionesMuestroteca {
    @Bean
    public static boolean estoyMuestroteca() {
        boolean resultado = false;

        return resultado;
    }
}

Now i call function from header.html

<th:block th:text="${FuncionesMuestroteca.estoyMuestroteca()}"/>

In POM.xml i have imported thymeleaf-extras version 2.1.0.RELEASE and thymeleaf 2.1.5

<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>2.1.0.RELEASE</version>

And here the StackTrace

org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method estoyMuestroteca() on null context object

If you need anything else, do not hesitate to tell me and I'll put it. Thank you in advance.

new StackTrace:

 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'estoyMuestroteca' available

HTML Edit

<div th:text="${@estoyMuestroteca.estoyMuestroteca()}"></div>

Method actual

@Configuration
public class FuncionesMuestroteca {
    @Bean(name = "estoyMuestroteca")
    public boolean estoyMuestroteca() {
        boolean resultado = false;

        return resultado;
    }
}

I add an image of the folder structure of the project

V.Vallejo
  • 17
  • 1
  • 1
  • 9

2 Answers2

13

A couple things here. First, the static method:

public class FuncionesMuestrotecaUtilidad { //note name change

    public static boolean estoyMuestroteca() {
         return false; //don't need to create extra variables if this is what you need
    }
}

In your HTML, you can use the proper syntax for calling a static method:

<th:block th:text="${T(com.package.FuncionesMuestrotecaUtilidad).estoyMuestroteca()}">
<!-- do whatever -->
</th:block>

Replace com.package with your package name. Also, this does not need the @Bean or @Configuration annotation - you are just calling a static method directly.

The major caveat to all this is that there might be a better way to design the code without using static methods. But that is outside the scope of this question.

Lastly, it makes a lot of sense to move to a newer version of Thymeleaf. It's far faster, less restrictive, and has more features.

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
  • Thank you very much, this has solved my problem. But I would like to follow your recommendations. I am trying to change the version of thymeleaf – V.Vallejo Jan 29 '18 at 07:58
  • @bphilipnyc what is the way to call a non-static function from a template? – AKrush95 Jan 29 '18 at 16:07
  • Not really the way you want to go. You want to have your controller add the variable to the model and have Thymeleaf (or whatever framework you're using) simply display the value. This keeps your view far simpler. Take a look at the Thymeleaf docs and you'll see that this is the general idea. – riddle_me_this Jan 29 '18 at 16:13
  • A major strength of Thymeleaf is simply passing on the HTML template to a UI person (someone who doesn't necessarily need to know a thing about Java). So keeping complex logic out of it will save you lots of trouble. – riddle_me_this Jan 29 '18 at 16:15
  • What does T stands for @vphilipnyc ? in the function – Parameshwar Jul 20 '22 at 12:07
  • Type. Common usage in Java – riddle_me_this Jul 21 '22 at 23:10
2

Here is a way to call a method that includes parameters from the view: 1: Create an interface have method that includes parameters(If the method not includes parameter, remove the parameters in example function):

public interface RewriteUrl {
    String rewriteUrl(String name, Integer id);
}

2: In beans's configuration file, add new bean implement that interface, as follows:

@Bean(name = "rewriteSongUrl")
    public RewriteUrl rewriteSongUrl() {
        return (String name, Integer id) -> {
            String url = name + "." + id + ".html";
            return getRewriteUrl(url);
        };
    }

3: In view, call bean:

<a th:href="${@rewriteSongUrl.rewriteUrl(singer.name, singer.id)}"></a>