maven Springboot控制器重定向不工作

apeeds0o  于 8个月前  发布在  Maven
关注(0)|答案(5)|浏览(89)

我正在尝试使用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>

请帮帮我
先谢了。

jw5wzhpr

jw5wzhpr1#

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

vc9ivgsu2#

尝试使用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
    );
}
zqdjd7g9

zqdjd7g93#

如果你想在springboot中使用“redirect:“,你必须注入“InternalResourceViewResolver”。虽然Spring Boot提供了自动配置,但如果你使用“@EnableWebMvc”,“InternalResourceViewResolver”不会注入。看这里

ecfdbz9o

ecfdbz9o4#

如果你试图重定向,但它不起作用,记住你不能放@ResponseBody注解。
所以,这个不会工作:

@PostMapping(value = "/agregar")
@ResponseBody // Annotation here
public String guardarProducto() {
    // magic here
    return "redirect:/productos/mostrar";
}

但这将做(删除@ResponseBody注解):

@PostMapping(value = "/agregar")
public String guardarProducto() {
    // magic here
    return "redirect:/productos/mostrar";
}

更多关于Sping Boot here中重定向的信息。

uurity8g

uurity8g5#

问题是通过客户端的xmlhttprequest对象进行调用。我必须处理事件的完成。我去掉了对客户机的$http.get调用,改为使用window.location.href。我的控制器重定向,页面自动加载。

相关问题