Springboot Controller redirect not working

I am trying a quick prototype using Spring boot.

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!!!! print statement. However, the nextPage doesn't load. If I don't use redirect and I type http://localhost:8080/gotoNextPage, the HTML content loads fine. Also, the index.html also loads fine

I am using spring boot mvc, thymeleaf, angularjs

My project structure is shown below: enter image description here

My POM XML file dependencies are shown below:

<?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.

Answers


I'd try to simplify the method to use Model and return a simple 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

@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. 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. 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.look here


Need Your Help

Java: Apache POI: Can I get clean text from MS Word (.doc) files?

java text ms-word apache-poi extraction

The strings I'm (programmatically) getting from MS Word files when using Apache POI are not the same text I can look at when I open the files with MS Word.

Poor execution plans when using a filter and CONTAINSTABLE in a query

sql sql-server-2008 full-text-search sql-execution-plan

We have an interesting problem that I was hoping someone could help to shed some light on. At a high level the problem is as below: