gpt4 book ai didi

java - Spring Boot中的多个index.html

转载 作者:行者123 更新时间:2023-11-30 06:49:39 26 4
gpt4 key购买 nike

我有一个简单的 Spring Boot 应用程序,在我的 web 应用程序中有两个 html。 enter image description here

在我的 Controller 中,我编写了如下映射:

@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]

我的代码有什么问题吗?

最佳答案

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

你应该有

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

然后从你的 Controller 使用(你说得对)

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

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

关于java - Spring Boot中的多个index.html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43072061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com