使用Ajax的Thymeleaf无法正常工作

时间:2021-05-29 21:29:23

I'm trying to use Thymeleaf fragments with Ajax on my project and I have this method on my controller:

我正在尝试在我的项目中使用带有Ajax的Thymeleaf片段,并且我在我的控制器上使用了这个方法:

public def displayInfo(Model model) {
    log.info("Guests method!")
    model.addAttribute("content", "testing ajax");
    "fragments/content::form-basic";
}

And I get this message before running the application:

我在运行应用程序之前收到此消息:

WARNING: The [displayInfo] action in [ca.capilanou.hrpay.workload.NonInstructionalWorkloadController] accepts a parameter of type [org.springframework.ui.Model].  Interface types and abstract class types are not supported as command objects.  This parameter will be ignored.

    public def displayInfo(Model model) {
    ^

On the html where I want to add the fragment by ajax I have this just for testing:

在html我想通过ajax添加片段我只是为了测试:

<button id="bt1" onclick="$('#content').load('/nonInstructionalWorkload/displayInfo');">Load Frag 1</button>
<div id="content"></div>

What is happening is that I get the "Guests method!" message on the console, which means that it's reaching the controller, but when it tries to do:

发生的事情是我得到了“宾客方法!”控制台上的消息,这意味着它正在到达控制器,但是当它尝试执行时:

model.addAttribute("content", "testing ajax");

I get a nullPointerException because the model parameter is coming null. So I tried to comment this line and just return the fragment I want to display. This is the fragment I have:

我得到一个nullPointerException,因为model参数为null。所以我试着评论这一行,然后返回我想要显示的片段。这是我的片段:

<th:block th:fragment="content" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
    <div th:text="${content}"></div>
</th:block>

I tried to put the ${content} text hard coded when commenting the model.addAttribute line, but I'm not getting anything back on my screen.

我在评论model.addAttribute行时尝试将$ {content}文本硬编码,但我的屏幕上没有任何回复。

What do I need to do to fix the "WARNING" I'm getting and also the to be able to see the fragment being shown on the right place?

我需要做些什么来修复我正在获得的“警告”,以及能够看到片段显示在正确的位置?

1 个解决方案

#1


0  

I was able to solve it this way:

我能够这样解决它:

Instead of using this method:

而不是使用此方法:

public def displayInfo(Model model) {
    log.info("Guests method!")
    model.addAttribute("content", "testing ajax");
    "fragments/content::form-basic";
}

I'm now using this:

我现在正在使用这个:

ModelAndView addWorkItem() {
        log.info("Display Fragment using Ajax")
        ModelAndView modelAndView = new ModelAndView("/fragments/content :: content")
        modelAndView.addObject("content", "Hello World!")
        return modelAndView
    }

We need to return the ModelAndView. If there anything you need to use on the screen you can addObject adding its attributes.

我们需要返回ModelAndView。如果您需要在屏幕上使用任何内容,则可以添加添加其属性的addObject。

By doing this I avoid the Model, so I get rid of the Warning issue.

通过这样做我避免模型,所以我摆脱了警告问题。

That's it. I wasn't able to find this solution anywhere.. I found pieces in different articles.

而已。我无法在任何地方找到这个解决方案..我在不同的文章中找到了作品。

https://github.com/dandelion/dandelion/issues/28

http://www.marcelustrojahn.com/2016/08/spring-boot-thymeleaf-fragments-via-ajax/

http://www.xpadro.com/2014/02/thymeleaf-integration-with-spring-part-2.html

#1


0  

I was able to solve it this way:

我能够这样解决它:

Instead of using this method:

而不是使用此方法:

public def displayInfo(Model model) {
    log.info("Guests method!")
    model.addAttribute("content", "testing ajax");
    "fragments/content::form-basic";
}

I'm now using this:

我现在正在使用这个:

ModelAndView addWorkItem() {
        log.info("Display Fragment using Ajax")
        ModelAndView modelAndView = new ModelAndView("/fragments/content :: content")
        modelAndView.addObject("content", "Hello World!")
        return modelAndView
    }

We need to return the ModelAndView. If there anything you need to use on the screen you can addObject adding its attributes.

我们需要返回ModelAndView。如果您需要在屏幕上使用任何内容,则可以添加添加其属性的addObject。

By doing this I avoid the Model, so I get rid of the Warning issue.

通过这样做我避免模型,所以我摆脱了警告问题。

That's it. I wasn't able to find this solution anywhere.. I found pieces in different articles.

而已。我无法在任何地方找到这个解决方案..我在不同的文章中找到了作品。

https://github.com/dandelion/dandelion/issues/28

http://www.marcelustrojahn.com/2016/08/spring-boot-thymeleaf-fragments-via-ajax/

http://www.xpadro.com/2014/02/thymeleaf-integration-with-spring-part-2.html