如何从ajax调用到Spring控制器来呈现新的jsp页面

时间:2022-09-13 20:01:01

I am using Spring I have

我用的是春天

Anchor tags list like below and i need to make post call so i used ajax query.

锚标记列表如下所示,我需要进行post调用,因此使用了ajax查询。

<td><a href="#" onclick='doAjax(${taskDetail.taskId})'> ${taskDetail.taskTitle}</a></td> 

in my same jsp i have ajax call as below

在同一个jsp中,我有如下所示的ajax调用

function doAjax(x) {

    alert(x);

    $.ajax({
    type: "POST",
    url: "getTaskDetail",
    data: { taskId: x }
       }
    })
  }

now my controller has

现在我的控制器

@RequestMapping(value = "/getTaskDetail", method = RequestMethod.POST)
    public String getTaskDetail(@RequestParam String taskId, Model model) {
    System.out.println("i am herer......");

        try {


            TaskDet taskDet = getTaskinformation(Integer.parseInt(taskId));


            model.addAttribute("taskDet", taskDet);

            return "showtaskpage";
        } catch (Exception e) {
            System.out.println(e.getMessage());
                        }
    }

now my "showtaskpage" itself is .jsp page.

现在我的“showtaskpage”本身就是.jsp页面。

now the problem is that , everything works like charm , but when it returns showtaskpage, it is not getting rentered in new jsp page , I am still in the previous page and when i click this anchor link , it goes to the controller and get response but i will get .jsp full page as data format , we can see it using firebug.

现在的问题是,一切都像魅力,但当它返回showtaskpage,它不是让房东新jsp页面,我仍然在前面的页面,当我点击这个锚的链接,它的控制器,得到反应但我会. jsp页面的数据格式,使用firebug我们可以看到它。

I want to be shown as http://localhost:8080/mydummy/showtaskpage

我希望显示为http://localhost:8080/mydummy/showtaskpage

Thanks in advance.

提前谢谢。

2 个解决方案

#1


3  

I have modified as below

我已经修改如下。

$.ajax({
type: "POST",
url: "getTaskDetail",
data: { taskId: x },
success : function(data) {
    alert('success');
    document.open();
    document.write(data);
    document.close();
}
})

now i what happens is it will render as new JSP page working .

现在发生的情况是,它将作为新的JSP页面呈现。

#2


0  

The way of return ModelAndView

返回模型和视图的方法

@RequestMapping(value = "/getTaskDetail", method = RequestMethod.POST)
public String getTaskDetail(@RequestParam String taskId, ModelAndView mav) {
    System.out.println("i am herer......");

    if (!user.isAuthenticated()) {
        mav.setViewName("redirect:http://localhost:8080/mydummy/showtaskpage");
        return mav;
    }

    mav.setViewName("showtaskpage");
    mav.addObject("wxyz", listService.getLists());

    return mav;
}

The way of return String

返回字符串的方式

@RequestMapping(value = "/getTaskDetail", method = RequestMethod.POST)
public String getTaskDetail(@RequestParam String taskId, ModelAndView mav) {

    if (!user.isAuthenticated()) {
        return "redirect:http://localhost:8080/mydummy/showtaskpage";
    }

    model.addAttribute("wxyz", listService.getLists());

    return "showtaskpage";
}

#1


3  

I have modified as below

我已经修改如下。

$.ajax({
type: "POST",
url: "getTaskDetail",
data: { taskId: x },
success : function(data) {
    alert('success');
    document.open();
    document.write(data);
    document.close();
}
})

now i what happens is it will render as new JSP page working .

现在发生的情况是,它将作为新的JSP页面呈现。

#2


0  

The way of return ModelAndView

返回模型和视图的方法

@RequestMapping(value = "/getTaskDetail", method = RequestMethod.POST)
public String getTaskDetail(@RequestParam String taskId, ModelAndView mav) {
    System.out.println("i am herer......");

    if (!user.isAuthenticated()) {
        mav.setViewName("redirect:http://localhost:8080/mydummy/showtaskpage");
        return mav;
    }

    mav.setViewName("showtaskpage");
    mav.addObject("wxyz", listService.getLists());

    return mav;
}

The way of return String

返回字符串的方式

@RequestMapping(value = "/getTaskDetail", method = RequestMethod.POST)
public String getTaskDetail(@RequestParam String taskId, ModelAndView mav) {

    if (!user.isAuthenticated()) {
        return "redirect:http://localhost:8080/mydummy/showtaskpage";
    }

    model.addAttribute("wxyz", listService.getLists());

    return "showtaskpage";
}