春季启动时多个index.html

时间:2017-03-28 14:18:10

标签: java spring spring-mvc spring-boot

我的webapp中有两个html的简单弹簧启动应用程序。 enter image description here

在我的控制器中,我写了如下的映射:

@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";
}
}

我也有属性:

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

当我通过浏览器localhost:8080 / - 我得到我的index.html页面 但是当我通过localhost:8080 / anotherIndex我有例外:

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]

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

主要问题是你没有使用在Spring Boot中为Web应用程序定义的文件夹结构。

你应该

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

然后从您的控制器使用(您拥有它)

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

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