java - Spring boot 中不呈现 Jsp View 页面。如何解决?

标签 java ajax spring spring-boot

我正在尝试发出一个将值发送到我的 spring boot 的 ajax 请求。但是,一旦我合并了 ajax 调用并将值成功传递给 java,它就不会查看 jsp 页面。我相信问题出在方法本身,但我不完全确定。我再次传递了值,但是每当我使用 ModelAndViewModel 时,它似乎都不会相应地更改页面。

JSP:

 <script>
        var chaptersTest = ${ chapters };
        var totalChapters = chaptersTest.length;
        var codeBlock;

        for (var i = 0; i < totalChapters; i++) {

            codeBlock =


                '<div class="col-md-4">' +

                ' <a class="chapter_link" data-link="' + chaptersTest[i] + '" style="text-decoration: none; color: white"><div class="box warning">' +

                '  <h3>' + chaptersTest[i] + '</h3>' +

                '  </div></a></div>';

            $("#chapterArea").append(codeBlock);
        }


        //clicked links

        $('.chapter_link').click(function () {
            console.log("YoUR fUNCtION IS cLICKED");
            doSomething();
        });



        function doSomething() {
            var search2 = {
                "chapter": "riley"
            }

            $.ajax({
                type: "POST",
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                url: "/ajax",
                data: JSON.stringify(search2),
                success: function (result) {
                    console.log("It somewhat worked");
                }

            });

        }



    </script>

Java:

@RequestMapping(value = "/ajax", method = RequestMethod.POST)
    public @ResponseBody String sectionView(@RequestBody BEntity benny, HttpServletRequest request) {
        String temp = benny.getChapter();
        ModelAndView secView = new ModelAndView();

        try {
            secView.setViewName("viewsections.jsp");
            //It is not sending the jsp to change the page

        } catch (Exception e) {
            secView.setViewName("error.jsp");

        }


        System.out.println("Test 1");
        return secView;
    }


对象:



public class BEntity {

    private String search; 
    private String chapter;
    private String section; 

    public String getSection() {
        return section; 
    }


    public String getChapter() {
        return chapter; 
    }

    public String getSearch() {
        return search;
    }

    @Override
    public String toString() {
        return "This was searched: " + search;
    } 

最佳答案

在您的 Controller 中,删除@ResponseBody 注释并将返回类型更改为ModelAndView。在您的 ajax 代码中将 dataType 更改为 html

如果您没有正确配置 View ,请在您的 application.properties 中将前缀和后缀设置为:

spring.mvc.view.prefix: /views/
spring.mvc.view.suffix: .jsp

spring.mvc.view.prefix 是您的 jsp 文件所在文件夹的路径。

@RequestMapping(value = "/ajax", method = RequestMethod.POST)
public ModelAndView sectionView(@RequestBody BEntity benny, HttpServletRequest request) {
    String temp = benny.getChapter();
    ModelAndView secView = new ModelAndView();
    try {
        secView.setViewName("viewsections");
        //It is not sending the jsp to change the page
    } catch (Exception e) {
        secView.setViewName("error");
    }
    System.out.println("Test 1");
    return secView;
}

关于java - Spring boot 中不呈现 Jsp View 页面。如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59430223/

相关文章:

java - Java 中的二维数组

php - 创建 Like 函数 php mysql ajax

java - DWR 对 JavaScript 变量的响应

javascript - 我的 Ajax 代码无法在 codeigniter 中发布数据库中的数据

java - app-context.xml 的完整 Java 配置,包括 jdbc 和 tx 指令等

Spring Social/connect 返回 HTTP 404

java - 为什么 Intellij IDEA 无法识别 ResourceBundleMessageSource 类的 defaultEncoding 字段?

java - 如何从同一实体的数组列表创建不同的实体实例

java - 使用 Apache Phoenix JDBC 驱动程序时如何配置queueSize和ThreadPoolSize?

java - 为什么此代码有这么多错误?