java - Spring Boot Controller 重定向不起作用

标签 java spring maven spring-mvc spring-boot

我正在尝试使用 Spring 引导的快速原型(prototype)。

我有以下 Controller 代码减去导入

@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,在我的服务器中,我看到了 Inside 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>

请帮忙。

提前致谢。

最佳答案

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

关于java - Spring Boot Controller 重定向不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33327678/

相关文章:

java - java中如何连接数组中的字符串

java - Maven 依赖项失败并出现 501 错误

java - 是否可以在 Feign 客户端上使用 Spring @Cache*?

java - Jenkins 运行所有组而不是指定组作为目标

java - 有没有办法从 Maven 构建中获得一致的 MD5 哈希?

java - 删除 ArrayList 中重复项的时间复杂度

java - 检查 BlackBerry 应用程序是否已安排

java - 在实现类或接口(interface)中注释方法?

java - 从依赖项目中过滤 Spring XML 文件

java.lang.NoSuchMethodError : org. hibernate.annotations.common.reflection.java.JavaReflectionManager.setMetadataProvider