我正在尝试使用Sping 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>
请帮帮我
先谢了。
5条答案
按热度按时间jw5wzhpr1#
我会尝试简化方法,使用
Model
并返回一个简单的String
:vc9ivgsu2#
尝试使用RedirectView explicite
zqdjd7g93#
如果你想在springboot中使用“redirect:“,你必须注入“InternalResourceViewResolver”。虽然Spring Boot提供了自动配置,但如果你使用“@EnableWebMvc”,“InternalResourceViewResolver”不会注入。看这里
ecfdbz9o4#
如果你试图重定向,但它不起作用,记住你不能放
@ResponseBody
注解。所以,这个不会工作:
但这将做(删除
@ResponseBody
注解):更多关于Sping Boot here中重定向的信息。
uurity8g5#
问题是通过客户端的xmlhttprequest对象进行调用。我必须处理事件的完成。我去掉了对客户机的$http.get调用,改为使用window.location.href。我的控制器重定向,页面自动加载。