[英]Springboot Controller redirect not working
I am trying a quick prototype using Spring boot. 我正在尝试使用Spring启动的快速原型。
I have the following Controller code minus the imports 我有以下控制器代码减去导入
@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;
}
}
From my HTML, I invoke the /gotoNextPage and in my Server, I see the Inside gotoNextPage!!!! 从我的HTML,我调用/ gotoNextPage,在我的服务器中,我看到里面的gotoNextPage !!!! print statement.
打印声明。 However, the nextPage doesn't load.
但是,nextPage不会加载。 If I don't use redirect and I type http://localhost:8080/gotoNextPage , the HTML content loads fine.
如果我不使用重定向并输入http:// localhost:8080 / gotoNextPage ,则HTML内容加载正常。 Also, the index.html also loads fine
此外,index.html也加载正常
I am using spring boot mvc, thymeleaf, angularjs 我正在使用spring boot mvc,thymeleaf,angularjs
My project structure is shown below: enter image description here 我的项目结构如下所示: 在此处输入图像描述
My POM XML file dependencies are shown below: 我的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>
Please help. 请帮忙。
Thanks in advance. 提前致谢。
I'd try to simplify the method to use Model
and return a simple String
: 我试着简化使用
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";
}
Try to use RedirectView explicite 尝试使用RedirectView explicite
@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
);
}
The problem was making a call through xmlhttprequest object on the client end. 问题是通过客户端上的xmlhttprequest对象进行调用。 I had to handle the completion of the event.
我不得不处理事件的完成。 I got rid of the $http.get call on the client and did a window.location.href instead.
我在客户端上删除了$ http.get调用并改为使用了window.location.href。 My controller redirects and the page loads automatically.
我的控制器重定向,页面自动加载。
If you want to use "redirect:" with springboot,You must inject "InternalResourceViewResolver".Although Spring Boot provides auto-configuration,if you use "@EnableWebMvc","InternalResourceViewResolver" will not inject. 如果你想在springboot中使用“redirect:”,你必须注入“InternalResourceViewResolver”。尽管Spring Boot提供自动配置,但如果使用“@EnableWebMvc”,“InternalResourceViewResolver”将不会注入。 look here
看这里
If you are trying to redirect and it does not work, remember that you mustn't put the @ResponseBody
annotation. 如果您尝试重定向但它不起作用,请记住您不能放置
@ResponseBody
注释。
So, this will not work: 所以,这是不行的:
@PostMapping(value = "/agregar")
@ResponseBody // Annotation here
public String guardarProducto() {
// magic here
return "redirect:/productos/mostrar";
}
But this will do (remove the @ResponseBody
Annotation): 但这样做 (删除
@ResponseBody
Annotation):
@PostMapping(value = "/agregar")
public String guardarProducto() {
// magic here
return "redirect:/productos/mostrar";
}
More info about redirect in Spring Boot here . 更多信息关于春天启动重定向这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.