java – Springboot Controller重定向无法正常工作

我正在尝试使用 Spring启动的快速原型.

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

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

请帮忙.

提前致谢.

最佳答案
我试着简化使用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 – Springboot Controller重定向无法正常工作 - 乐贴网