Java Springboot 控制器重定向不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33327678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:34:32  来源:igfitidea点击:

Springboot Controller redirect not working

javaspringmavenspring-mvcspring-boot

提问by supreethmurthy

I am trying a quick prototype using Spring boot.

我正在尝试使用 Spring Boot 的快速原型。

I have the following Controller code minus the imports

我有以下控制器代码减去导入

@EnableAutoConfiguration
@RestController
@Controller
public class IndexController{
    @RequestMapping(value = "/")
    public ModelAndView firstPage(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("message", Calendar.getInstance().getTime().toString());
        return mv;
    }
    @RequestMapping(value="/gotoNextPage",method = RequestMethod.GET)
    public  ModelAndView gotoNextPage(HttpServletRequest request, HttpServletResponse response){
        System.out.println("Inside gotoNextPage!!!!!!");
        ModelAndView mv = new ModelAndView("redirect:nextpage");
        mv.addObject("message", "next page");
        return mv;
    }
}

From my HTML, I invoke the /gotoNextPage and in my Server, I see the Inside gotoNextPage!!!! print statement. However, the nextPage doesn't load. If I don't use redirect and I type http://localhost:8080/gotoNextPage, the HTML content loads fine. Also, the index.html also loads fine

在我的 HTML 中,我调用了 /gotoNextPage,在我的服务器中,我看到了内部 gotoNextPage!!!!打印声明。但是,nextPage 不会加载。如果我不使用重定向并键入http://localhost:8080/gotoNextPage,则 HTML 内容加载正常。此外,index.html 也可以正常加载

I am using spring boot mvc, thymeleaf, angularjs

我正在使用 spring boot mvc、thymeleaf、angularjs

My project structure is shown below: enter image description here

我的项目结构如下图: 在此处输入图片描述

My POM XML file dependencies are shown below:

我的 POM XML 文件依赖项如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>ng-spring-boot-helloworld</groupId>
    <artifactId>ng-spring-boot-helloworld</artifactId>
    <version>1.0.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.0.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.hsqldb</groupId>
                <artifactId>hsqldb</artifactId>
                <version>2.3.2</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
                <version>1.2.7.RELEASE</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

</project>

Please help.

请帮忙。

Thanks in advance.

提前致谢。

采纳答案by daniel.eichten

I'd try to simplify the method to use Modeland return a simple String:

我会尝试简化使用方法Model并返回一个简单的String

@RequestMapping(value="/gotoNextPage",method = RequestMethod.GET)
public String gotoNextPage(Model model){
    LOG.debug("Inside gotoNextPage!!!!!!");
    model.addAttribute("message", "next page");
    return "redirect:nextpage.html";
}

回答by Ralph

Try to use RedirectViewexplicite

尝试使用RedirectView显式

@RequestMapping(value="/gotoNextPage",method = RequestMethod.GET)
public  ModelAndView gotoNextPage(HttpServletRequest request, HttpServletResponse response){
    System.out.println("Inside gotoNextPage!!!!!!");

    ModelMap model = new ModelMap();
    model.add("message", "next page");
    return new ModelAndView(
       new RedirectView("/nextpage", true),
       //or new RedirectView("/nextpage.html", true),
       model
    );
}

回答by supreethmurthy

The problem was making a call through xmlhttprequest object on the client end. I had to handle the completion of the event. I got rid of the $http.get call on the client and did a window.location.href instead. My controller redirects and the page loads automatically.

问题是通过客户端上的 xmlhttprequest 对象进行调用。我不得不处理事件的完成。我摆脱了客户端上的 $http.get 调用,而是做了一个 window.location.href 。我的控制器重定向并自动加载页面。

回答by hahugao

If you want to use "redirect:" with springboot,You must inject "InternalResourceViewResolver".Although Spring Boot provides auto-configuration,if you use "@EnableWebMvc","InternalResourceViewResolver" will not inject.look here

如果你想在springboot中使用“redirect:”,你必须注入“InternalResourceViewResolver”。虽然Spring Boot提供了自动配置,如果你使用“@EnableWebMvc”,“InternalResourceViewResolver”不会注入。看这里

回答by Luis Cabrera Benito

If you are trying to redirect and it does not work, remember that you mustn't put the @ResponseBodyannotation.

如果您尝试重定向并且它不起作用,请记住您不能放置@ResponseBody注释。

So, this will notwork:

所以,这将无法正常工作:

@PostMapping(value = "/agregar")
@ResponseBody // Annotation here
public String guardarProducto() {
    // magic here
    return "redirect:/productos/mostrar";
}

But this will do(remove the @ResponseBodyAnnotation):

但这会做(删除@ResponseBody注释):

@PostMapping(value = "/agregar")
public String guardarProducto() {
    // magic here
    return "redirect:/productos/mostrar";
}

More info about redirect in Spring Boot here.

有关 Spring Boot 中重定向的更多信息,请访问此处