简体   繁体   English

更改页面 spring 引导应用程序

[英]Changing pages spring boot application

I having problem changing pages.我在更改页面时遇到问题。 So what I have is a button and when that user press that button a ajax Post is called.所以我有一个按钮,当用户按下该按钮时,会调用 ajax Post。 Here the example:这里的例子:

 $.ajax({
        contentType: "application/json",
        type: "POST",
        data: JSON.stringify(project),
        url: "/saveProject",
        success: function (data) {
            console.log('done');
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log('error while post');
        }
    });

@RequestMapping(value = "/saveProject", method = RequestMethod.POST)
public @ResponseBody
String saveProject(@RequestBody Project newProject, Authentication authentication) {
    projectService.saveProjectNew(newProject, authentication);

    return "mywork.html";

}

So in the end I want to be redirect to mywork.html from the page I'm currently on.所以最后我想从我当前所在的页面重定向到 mywork.html 。 However nothing happens and I stay on same page.但是什么也没发生,我留在同一页面上。 I'm probably missing something that I don't know.我可能错过了一些我不知道的东西。 Quiet new to this.对此很陌生。

To redirect the page into the mywork.html You need to write the code once you get the response from the Ajax call So under the success function of Ajax you should use To redirect the page into the mywork.html You need to write the code once you get the response from the Ajax call So under the success function of Ajax you should use

windows.location.href = "Your context path"+"/mywork.html";

Please refer the reference code below:请参考以下参考代码:

$.ajax({
    contentType: "application/json",
    type: "POST",
    data: JSON.stringify(project),
    url: "/saveProject",
    success: function (data) {
        windows.location.href = "Your context path"+"/mywork.html";

    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.log('error while post');
    }
});

Here the spring web client code will not divert the call to the mywork.html.这里的 spring web 客户端代码不会将调用转移到 mywork.html。

All the call will be diverted only through the Ajax call.所有呼叫将仅通过 Ajax 呼叫转移。

return "mywork.html";

This code is only used to model your response which been retrieved after calling the endpoint.此代码仅用于 model 您在调用端点后检索到的响应。

Http redirection could be triggered from both the back-end as well as the front-end ajax code that you have posted. Http 重定向可以从后端以及您发布的前端 ajax 代码触发。

For the redirection to work from the ui, you can add the window redirection like @Anurag pointed out in his answer on the ajax success callback.为了从 ui 进行重定向,您可以添加 window 重定向,就像@Anurag 在他对 ajax 成功回调的回答中指出的那样。

But in your example you are trying to redirect the user to a new page from the backend endpoint itself.但是在您的示例中,您试图将用户从后端端点本身重定向到新页面。 So provided that you already have a controller returning the view for the mapping /mywork.html in order for the redirect to work from the spring backend side, you need to do the following:因此,如果您已经有一个 controller 返回映射/mywork.html的视图,以便重定向从 spring 后端工作,您需要执行以下操作:

@RequestMapping(value = "/saveProject", method = RequestMethod.POST)
public String saveProject(@RequestBody Project newProject, Authentication authentication) {
    projectService.saveProjectNew(newProject, authentication);
    return "redirect:/mywork.html";

}

or using ResponseEntity like:或使用ResponseEntity像:

HttpHeaders headers = new HttpHeaders();
headers.setLocation(URI.create(newUrl));
return new ResponseEntity<>(headers, HttpStatus.MOVED_PERMANENTLY);

In your code you were using the annotation @ResponseBody for the controller method which basically makes the endpoint a rest endpoint returning json by default.在您的代码中,您将注释@ResponseBody用于 controller 方法,该方法基本上使端点成为 rest 端点,默认情况下返回 json。 So, for redirection to work remove the annotation and make it a normal controller method returning view.因此,要使重定向工作,请删除注释并使其成为正常的 controller 方法返回视图。

Still if you want to redirect from a rest endpoint then use HttpServletResponse like:不过,如果您想从 rest 端点重定向,请使用HttpServletResponse ,例如:

@RequestMapping(value = "/saveProject", method = RequestMethod.POST)
public @ResponseBody String saveProject(@RequestBody Project newProject, Authentication authentication, HttpServletResponse response) {
    projectService.saveProjectNew(newProject, authentication);
    response.sendRedirect("url-here");   
}

For more information Link .欲了解更多信息链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM