【问题标题】:Error trying call method from view Thymeleaf Spring尝试从 Thymeleaf Spring 视图调用方法时出错
【发布时间】:2018-01-23 13:23:10
【问题描述】:

当我尝试从视图调用方法时遇到问题。

我的带有方法的 Java 类

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

        return resultado;
    }
}

现在我从 header.html 调用函数

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

在 POM.xml 中,我导入了 thymeleaf-extras 版本 2.1.0.RELEASE 和 thymeleaf 2.1.5

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

这里是 StackTrace

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

如果您需要其他任何内容,请随时告诉我,我会告诉您的。提前谢谢你。

新的 StackTrace:

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

HTML 编辑

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

方法实际

@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

【问题讨论】:

  • 删除public static boolean estoyMuestroteca()中的静态并在public class FuncionesMuestroteca 上添加@Configuration
  • 要直接访问 Spring bean,请使用 @。例如:&lt;div th:text="${@urlService.getApplicationUrl()}"&gt;...&lt;/div&gt; 您可以在docs 的第 5 点找到它的文档
  • 非常感谢帕特里克,我已经做出了你告诉我的更改,但现在错误是“没有名为 'estoyMuestroteca' 的 bean 可用”。是否有可能我必须在控制器中配置或添加 bean?
  • 你能添加堆栈跟踪吗
  • 对不起兄弟,我输入了新的答案,因为评论太长了

标签: java spring spring-mvc spring-boot thymeleaf


【解决方案1】:

这里有几件事。一、静态方法:

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
    }
}

在您的 HTML 中,您可以使用正确的语法来调用静态方法:

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

用你的包名替换com.package。此外,这不需要 @Bean@Configuration 注释 - 您只是直接调用静态方法。

所有这一切的主要警告是,可能有更好的方法来设计代码而不使用静态方法。但这超出了这个问题的范围。

最后,迁移到较新版本的 Thymeleaf 很有意义。它速度更快,限制更少,功能更多。

【讨论】:

  • 非常感谢,这解决了我的问题。但我想听从你的建议。我正在尝试更改百里香的版本
  • @bphilipnyc 从模板调用非静态函数的方法是什么?
  • 不是你想走的路。您想让控制器将变量添加到模型中,并让 Thymeleaf(或您正在使用的任何框架)简单地显示该值。这使您的视图更加简单。看看 Thymeleaf 文档,你会发现这是一般的想法。
  • Thymeleaf 的一个主要优势是将 HTML 模板简单地传递给 UI 人员(不一定需要了解 Java 的人)。因此,将复杂的逻辑排除在外将为您省去很多麻烦。
【解决方案2】:

这是一种调用包含视图参数的方法的方法: 1:创建一个接口有参数的方法(如果方法不包括参数,删除示例函数中的参数):

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

2:在beans的配置文件中,添加新的bean实现该接口,如下:

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

3:在视图中,调用bean:

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

【讨论】:

    猜你喜欢
    • 2012-05-09
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    相关资源
    最近更新 更多