3

I have simple spring boot application with two html inside my webapp. enter image description here

In my controller i have written mapping like:

@Controller
public class HtmlController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
    return "index";
}

@RequestMapping(value = "/anotherIndex", method = RequestMethod.GET)
public String anotherIndex() {
    return "anotherIndex";
}
}

Also i have properties:

spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html

When i pass in browser localhost:8080/ - i get my index.html page But when i pass localhost:8080/anotherIndex i have exception:

javax.servlet.ServletException: Circular view path [/anotherIndex.html]: would dispatch back to the current handler URL [/anotherIndex.html] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:205) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:145) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1282) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]

What's wrong with my code?

3
  • You have multiple typos? Look at the actual path in the exception; it seems that you have Spring Security in place but aren't handling the login page correctly. Commented Mar 28, 2017 at 14:22
  • There is a typo in your URL (localhost:8080/anoterIndex) missed the h letter =) Commented Mar 28, 2017 at 14:22
  • Sorry, it's a dummy code. I ask about situation at all :) Commented Mar 28, 2017 at 14:26

1 Answer 1

2

The main problem its you are not using the folder structure defined for a web application in Spring Boot.

You should have

src
   main
       resources
                templates
                         index.html
                         anotherIndex.html
                         anotherFolder
                             index.html
                         ...

Then from your Controller use (You have it right)

@RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
    return "index";
}

@RequestMapping(value = "/anotherIndex", method = RequestMethod.GET)
public String anotherIndex() {
    return "anotherIndex";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah i know about this approach. I do similar in another project, where suffix "/" and prefix "/index.html". But is any another solution exist? For example a.html and b.html have similar js and css and i don't wont to put their in another folders.
You can create a folder static at same level as templates, and inside the common css and js, then since your html templates make reference to those files

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.