【问题标题】:Springboot Controller redirect not workingSpring Boot 控制器重定向不起作用
【发布时间】:2016-01-24 11:39:26
【问题描述】:

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

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

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

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

我使用的是spring boot mvc、thymeleaf、angularjs

我的项目结构如下图所示: enter image description here

我的 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>

请帮忙。

提前致谢。

【问题讨论】:

  • 重定向是客户端,因此您在 redirect: 之后传递的内容应该导致有效的 URL 而不是视图名称...并且停止混合您正在混合 Spring Boot 1.2.0 和 1.2 的版本.7.将父级更改为 1.2.7 并删除其他地方的版本。
  • @M.Deinum:感谢您提出版本号的差异。我已经纠正了。我将重定向更改为 ModelAndView mv = new ModelAndView("redirect:nextpage.html");并且还在我的项目结构中的静态文件夹下移动了 nextpage.html。它不起作用。我还尝试了“重定向:/nextpage.html”。他们两个都没有工作。我试过localhost:8080/nextpage.html,内容加载正常。
  • 您同时拥有@RestController@Controller,就是这样...从您的返回类型来看,您需要@Controller 而不是@RestController@EnableAutoConfiguration 在这个类上也没用。

标签: java spring maven spring-mvc spring-boot


【解决方案1】:

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

所以,这不会工作:

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

但是这个可以(去掉@ResponseBody注解):

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

有关 Spring Boot here 中重定向的更多信息。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        我会尝试简化使用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";
        }
        

        【讨论】:

        • 我试过你的代码。那是行不通的。我已将 nextpage.html 放在静态文件夹中。该功能被击中,我看到了系统。但是页面没有重定向。
        • 看来您确实有不同的问题。你能检查 Http 标头吗?直接在浏览器中打开能得到下一页吗?
        • 是的。我可以直接在浏览器上打开页面。同样在我的控制器方法中,如果我不使用重定向,当我在浏览器上键入 /gotoNextPage 时,我可以直接调用 nextpage 的内容。令我困惑的另一件事是,如果我输入 redirect:www.google.com,我的浏览器上也看不到 google.com。控制器似乎能够重定向,但浏览器没有重新加载页面。我是否缺少属性文件中的任何配置?感谢您的帮助。
        • @supreethmurthy,您应该使用完整的 URL,例如 redirect:http://www.google.com 重定向到外部页面。
        • 我查看了 http 标头。我的状态为 200,这是正确的。但我正在客户端通过 XmlHttpRequest 进行调用。这会导致问题吗?
        【解决方案5】:

        尝试使用RedirectViewexplicite

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

        【讨论】:

        • 我尝试重定向到另一个控制器方法而不是 html 文件,并且重定向的控制器方法被命中。我从新方法中看到了一个系统。在新方法中,我尝试了重定向、转发以及像这样的通用方式。 ModelAndView mv = new ModelAndView("nextpage"); mv.addObject("消息", Calendar.getInstance().getTime().toString());返回 mv;这也行不通。从新控制器中,页面未加载。控制台也没有错误。
        • 它很旧,但读到这里我会说浏览器已经达到了最终结果,而你的 spring 应用程序继续执行..
        猜你喜欢
        • 2021-01-17
        • 2020-03-09
        • 1970-01-01
        • 2019-01-03
        • 2020-09-25
        • 2016-08-29
        • 2017-04-14
        • 1970-01-01
        相关资源
        最近更新 更多