【问题标题】:Ajax call to spring boot controller to redirecting a view对 Spring Boot 控制器的 Ajax 调用以重定向视图
【发布时间】:2019-01-28 22:35:12
【问题描述】:

我是 Spring Boot 框架的新手。 我有一个 jQuery 方法,它在单击 span 元素时被调用。在那个 jQuery 方法中,我有一个 ajax 调用,它调用 Spring Boot 控制器并将字符串作为参数传递。控制器正在获取从 ajax 调用传递的参数。但它并没有重定向到不同的视图。不同的视图是一个名为 ajaxView.html 的 html 页面。我也想为视图添加一个属性。任何帮助都会很棒。

这是我的 ajax 调用:

$(document).ready(function() {
$('.spanClass').on('click', '#id_1', function(event){   
     event.stopPropagation();

         var str = ((event.target.parentElement).parentElement.href);
            $.ajax({
                type: 'POST',
                url: "http://localhost:8080//span/view?term="+encodeURIComponent(str),
                contentType: 'text/plain',
                crossDomain: false,
                async:true,
                success:function(response) {        
                }
            });
  });
});

这是我的控制器方法:

@RequestMapping(method=RequestMethod.POST, value = "/span/view")
public @ResponseBody ModelAndView redirectAjax(String term, Model model) {

    Employee emp = new Employee();
    emp.setName(term);

    model.addAttribute("employee", emp);

    return new ModelAndView("redirect:/ajaxView");

}

【问题讨论】:

标签: java jquery ajax spring-boot thymeleaf


【解决方案1】:

您需要从您的 Ajax 成功函数中再次调用控制器来为您打开 ajaxView.html 页面。 :--

1.) 你的 ajax 调用应该是这样的:--

$.ajax({
                type: 'POST',
                url: "http://localhost:8080//span/view?term="+encodeURIComponent(str),
                contentType: 'text/plain',
                crossDomain: false,
                async:true,
                success:function(response) {  

                window.location = '/yourAjaxControllerName';
                }
            });

2.) 你的控制器:--

   @RequestMapping("/yourAjaxControllerName")
    public String getAjaxViewPage(HttpServletRequest request,Model model) {

       return "ajaxView";

     }

【讨论】:

    【解决方案2】:

    您需要在 Ajax 本身中执行此操作。

    在Ajax调用成功方法中需要重新发送响应。

    $(document).ready(function() {
    $('.spanClass').on('click', '#id_1', function(event){   
         event.stopPropagation();
    
             var str = ((event.target.parentElement).parentElement.href);
                $.ajax({
                    type: 'POST',
                    url: "http://localhost:8080//span/view?term="+encodeURIComponent(str),
                    contentType: 'text/plain',
                    crossDomain: false,
                    async:true,
                    success:function(emp) {     
                      alert(emp);  
                      window.location.href = '/JspControllerHandler?employee='+ JSON.stringify(emp); // redirect     //this would be GET
                    }
                });
      });
    });
    

    您的控制器会将员工数据返回给请求 Ajax。

    @RequestMapping(method=RequestMethod.POST, value = "/span/view")
    public @ResponseBody Employee redirectAjax(String term, Model model) {
        Employee emp = new Employee();
        emp.setName(term);
        return emp;
    }
    

    您的 Ajax 将负责重定向到另一个页面

      @RequestMapping("/JspControllerHandler")
        public String redirectJsp(HttpServletRequest request,@RequestParam("employee") String empStr) {
        Employee employee = null;
        try
        {
            ObjectMapper mapper = new ObjectMapper();
            employee = mapper.readValue(empStr, Employee.class);
             model.addAttribute("employee", employee );
        }
        catch(Exception ex)
        {
            System.out.println("Error while converting JSON string to employee object.");
            ex.printStackTrace();
        }
           return "jspView";
         }
    

    【讨论】:

    • 太棒了!但是如何访问从 window.location.href = '/JspControllerHandler?employee='+ emp; 传递的变量“emp”在控制器中,redirectJsp ?当我尝试访问 emp 属性时,我得到了 null 值。仅供参考 ajax 中的 emp 属性不为空。
    • @khalbali 代码已更新。立即尝试
    • 它不起作用。 emp 是一个对象。而且我想将其保留为对象而不是将其转换为字符串。我打算将该对象作为模型属性传递给 jspView.html 页面。
    • @khalibali 代码已更新。问题是您需要将该 JSON 转换为 Object 并传递给 spring 模型
    猜你喜欢
    • 1970-01-01
    • 2016-01-24
    • 2018-11-24
    • 2019-01-03
    • 2020-09-25
    • 2016-08-29
    • 2015-05-21
    • 2021-05-31
    相关资源
    最近更新 更多