3

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.

3
  • 2
    A redirect is client side, so what you pass after the redirect: should lead to a valid URL not a view name... And stop mixing versions you are mixing Spring Boot 1.2.0 and 1.2.7. Change the parent to 1.2.7 and remove the versions elsewhere. Commented Oct 25, 2015 at 8:40
  • @M.Deinum: Thanks for bringing up the discrepancy in version numbers. I have corrected that. I changed the redirect to say ModelAndView mv = new ModelAndView("redirect:nextpage.html"); and also moved the nextpage.html under the static folder in my project structure. It doesn't work. I also tried "redirect:/nextpage.html". Both of them didn't work. I tried localhost:8080/nextpage.html and the content loads up fine. Commented Oct 25, 2015 at 9:05
  • You have both @RestController and @Controller which is it... Judging from your return types you need @Controller not @RestController. Also the @EnableAutoConfiguration is useless on this class. Commented Oct 25, 2015 at 11:31

5 Answers 5

3

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";
}
Sign up to request clarification or add additional context in comments.

7 Comments

I tried your code. That isn't working. I have placed the nextpage.html in the static folder. The function is being hit and I see the sys out. But the page isn't redirecting.
It seems that you do have a different issue. Can you check the Http headers? Can you get the nextpage when you open it in the browser directly?
Yes. I am able to open the page directly on the browser. Also in my controller method, if I don't use redirect, I am able to invoke the contents of nextpage directly when I type /gotoNextPage on the browser. The other thing that is baffling me is if I type redirect:www.google.com, I don't see google.com on my browser either. It seems like the controller is able to redirect but the browser is not reloading the page. Am I missing any configuration in the property file? Thanks for your help.
@supreethmurthy, you should use full URL, like this redirect:http://www.google.com for redirect to external page.
I looked at the http headers. I am getting a status of 200, which is correct. But I am making a call through XmlHttpRequest on the client side. Could that be causing the problem?
|
2

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
    );
}

2 Comments

I tried redirecting to another controller method instead of an html file and the redirected controller method is hit. I see a sys out from the new method. In the new method, I tried redirect, forward and also generic way like this. ModelAndView mv = new ModelAndView("nextpage"); mv.addObject("message", Calendar.getInstance().getTime().toString()); return mv; This isn't working either. From the new controller, the page isn't loaded. There is no error on the console either.
It's old but reading this I would say that the browser has reached the final result, while your spring application continued the execution..
2

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

Comments

1

If you are trying to redirect and it does not work, remember that you mustn't put the @ResponseBody annotation.

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):

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

More info about redirect in Spring Boot here.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.