【问题标题】:Jsp view page is not rendered in Spring boot. How to solve it?Jsp 视图页面在 Spring Boot 中不呈现。如何解决?
【发布时间】:2020-04-13 06:15:45
【问题描述】:

我正在尝试发出一个向我的 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;
    } 

【问题讨论】:

    标签: java ajax spring spring-boot


    【解决方案1】:

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

    如果您没有正确配置视图,那么在您的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;
    }
    

    【讨论】:

    • 不,我们按照您指定的方式进行了操作,但仍然无法查看该页面。它只显示System.out.println()
    • 您是否正确配置了视图?比如,设置视图的前缀和后缀。
    • Gautum 不,即使我尝试了您建议的方式,它仍然无法呈现它。
    • 让我编辑我的答案。我也会添加属性文件。
    • Gautum 好的,但我仍然遇到同样的问题。
    猜你喜欢
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 2016-12-01
    相关资源
    最近更新 更多