【发布时间】: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