Thymeleaf - 方法中的参数始终为空

发布于 2025-01-16 05:08:40 字数 386 浏览 0 评论 0原文

我是百里香新手,遇到以下问题。我想在 html 模板中调用 java 方法,但该方法需要一个参数,但问题是无论我传递什么参数,结果始终为 null。

categories.html:

 <p th:text="${postsCount(1)}"></p>

类别控制器:

    @ModelAttribute("postsCount")
    public int getPostsCountInCategory(Integer categoryId) {
       return categoryService.getPostsCountInCategory(categoryId);
    }

I am new to thymeleaf and I have the following problem. I want to call a java method inside the html template but the method needs an argument but the issue is that no matter what I am passing for an argument the result is always null.

categories.html:

 <p th:text="${postsCount(1)}"></p>

categoryController:

    @ModelAttribute("postsCount")
    public int getPostsCountInCategory(Integer categoryId) {
       return categoryService.getPostsCountInCategory(categoryId);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风轻花落早 2025-01-23 05:08:40

我怀疑问题不在您的代码中,而是在这个注释中: @ModelAttribute("postsCount") ——它可能试图通过调用将 postsCount 放入模型中getPostsCountInCategory(null) 因为它不知道要提供什么值。

话虽如此,您确实不应该在 HTML 代码中调用数据库方法。不能保证它们只会被调用一次,并且您违反了模型/视图/控制器分离。您应该将这些值添加到您的模型中,并将其显示在您的页面上。

@Controller
public class Controller() {
    @Autowired private PostCounter postCounter;
    
    $Get("category/")
    public String loadView(Model model) {
        model.put("postCounts", postCounter.getPostsCountInCategory(1));
    }
}

在 HTML 中:

<p th:text="${postsCount}"></p>

I suspect the problem is not in your code, but in this annotation: @ModelAttribute("postsCount") -- which is probably trying to put postsCount onto the model by calling getPostsCountInCategory(null) because it doesn't know what value to supply.

That being said, you really shouldn't be calling database methods in your HTML code. There is no guarantee they'll only be called once, and you are violating the model/view/controller separation. You should be adding the values to your model, and displaying that on your page.

@Controller
public class Controller() {
    @Autowired private PostCounter postCounter;
    
    $Get("category/")
    public String loadView(Model model) {
        model.put("postCounts", postCounter.getPostsCountInCategory(1));
    }
}

And in the HTML:

<p th:text="${postsCount}"></p>
你没皮卡萌 2025-01-23 05:08:40

在 Thymeleaf 中,您不能只调用没有对象引用的方法。
你可以这样做:

Java代码:

//Make sure that, this class will be in IOC as bean
@Bean
public class PostCountHelper {
public int postCount(int count) {
//your handling logic goes here & count will have value which you passed
}
}

@Controller
public class PostCountController() {
private PostCountHelper postCountHelper;
public String loadView(Model model) {
model.put("postCountHelperReference", postCountHelper);
}
}

Thymeleaf代码:

<p th:text="${postCountHelperReference.postCount(1)}"

In Thymeleaf, you can not just call a method without object reference.
You can do something like this:

Java Code:

//Make sure that, this class will be in IOC as bean
@Bean
public class PostCountHelper {
public int postCount(int count) {
//your handling logic goes here & count will have value which you passed
}
}

@Controller
public class PostCountController() {
private PostCountHelper postCountHelper;
public String loadView(Model model) {
model.put("postCountHelperReference", postCountHelper);
}
}

Thymeleaf Code:

<p th:text="${postCountHelperReference.postCount(1)}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文