Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Ajax调用spring引导控制器重定向视图_Java_Jquery_Ajax_Spring Boot_Thymeleaf - Fatal编程技术网

Java Ajax调用spring引导控制器重定向视图

Java Ajax调用spring引导控制器重定向视图,java,jquery,ajax,spring-boot,thymeleaf,Java,Jquery,Ajax,Spring Boot,Thymeleaf,我是spring启动框架的新手。 我有一个jQuery方法,它在单击span元素时被调用。在这个jQuery方法中,我有一个ajax调用,它调用spring引导控制器并将字符串作为参数传递。控制器正在获取从ajax调用传递的参数。但它并没有重定向到不同的视图。不同的视图是一个名为ajaxView.html的html页面。我还想给视图添加一个属性。任何帮助都会很好 以下是我的ajax调用: $(document).ready(function() { $('.spanClass').on('cli

我是spring启动框架的新手。 我有一个jQuery方法,它在单击span元素时被调用。在这个jQuery方法中,我有一个ajax调用,它调用spring引导控制器并将字符串作为参数传递。控制器正在获取从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");

}

您需要从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';
                }
            });
   @RequestMapping("/yourAjaxControllerName")
    public String getAjaxViewPage(HttpServletRequest request,Model model) {

       return "ajaxView";

     }
2.)您的控制器:---

$.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';
                }
            });
   @RequestMapping("/yourAjaxControllerName")
    public String getAjaxViewPage(HttpServletRequest request,Model model) {

       return "ajaxView";

     }

您需要从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';
                }
            });
   @RequestMapping("/yourAjaxControllerName")
    public String getAjaxViewPage(HttpServletRequest request,Model model) {

       return "ajaxView";

     }
2.)您的控制器:---

$.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';
                }
            });
   @RequestMapping("/yourAjaxControllerName")
    public String getAjaxViewPage(HttpServletRequest request,Model model) {

       return "ajaxView";

     }

您需要在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;
}
您必须负责任地重定向到另一个页面

  @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";
     }

您需要在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;
}
您必须负责任地重定向到另一个页面

  @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";
     }

您不能从AJAX重定向到其他页面。您只需要通过脚本来处理它。您不能从AJAX重定向到其他页面。您只需要通过脚本来处理它。令人惊叹的!但是如何访问从window.location.href='/JspControllerHandler?employee='+emp;在控制器中,重定向JSP?当我试图访问emp属性时,得到了空值。仅供参考,ajax中的emp属性不为null。@代码已更新。现在试试看,它不起作用。emp是一个对象。我想把它保留为一个对象,而不是转换成一个字符串。我打算将该对象作为模型属性传递到jspView.html页面。@khalibali代码已更新。问题是,您需要将JSON转换为对象并传递给spring ModelAware!但是如何访问从window.location.href='/JspControllerHandler?employee='+emp;在控制器中,重定向JSP?当我试图访问emp属性时,得到了空值。仅供参考,ajax中的emp属性不为null。@代码已更新。现在试试看,它不起作用。emp是一个对象。我想把它保留为一个对象,而不是转换成一个字符串。我打算将该对象作为模型属性传递到jspView.html页面。@khalibali代码已更新。问题是您需要将JSON转换为对象并传递给spring模型