我正在创建一个登录页面,允许用户登录。但是,当我尝试通过谷歌浏览器访问http://localhost:8080/index时,我只看到“索引”,没有别的。但它应该显示我使用HTML和Bootstrap创建的登录页面。
以下是HomeController.java的代码
package com.userFront.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HomeController {
@RequestMapping("/")
public String home() {
return "redirect:/index";
}
@RequestMapping("/index")
public @ResponseBody String index() {
return "index";
}
}
您可以在此处找到所有项目文件: https://drive.google.com/open?id=1fxTWDo_3_iaS4zaav60KaT9OvB-kSjv6
更新:1 我被建议从方法索引()中删除@ResponseBody。但是当我这样做时,会出现以下问题:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Dec 11 17:52:53
There was an unexpected error (type=Internal Server Error, status=500).
Circular view path [index]: would dispatch back to the current handler URL [/index] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
更新2: 这是HomeController类的最新版本,我根据我从@vtx收到的建议进行了修改:
package com.userFront.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String home() {
return "redirect:/index";
}
@RequestMapping("/welcome")
public String index() {
return "index";
}
}
但是现在,出现了一个新错误,其中包含:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Dec 11 18:52:41
There was an unexpected error (type=Not Found, status=404).
No message available
更新3:正如@vtx所建议的那样,我试图清理安装Maven。但是,会发生以下错误:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building UserFront 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ userFront ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 30 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ userFront ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 9 source files to C:\Users\Kanon\eclipse-workspace\UserFront\target\classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.606 s
[INFO] Finished at: 2017-12-11T23:31:14+06:00
[INFO] Final Memory: 18M/227M
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "pom.xml" could not be activated because it does not exist.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project userFront: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
更新4:根据@vtx的建议,我已经完成了Maven的干净安装。
我关注了stackoverflow.com/questions/19655184 / ...和stackoverflow.com/questions/19655184 / ... Maven安装干净,没有任何错误。但是现在,我得到了“循环视图路径”错误。
更新5: @vtx建议我再次使用以下代码:
package com.userFront.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String home() {
return "redirect:/index";
}
@RequestMapping("/welcome")
public String index() {
return "index";
}
}
但是再一次,我收到以下错误:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Dec 12 00:27:42
There was an unexpected error (type=Not Found, status=404).
No message available
令人难以置信的沮丧。但我不会放弃!
答案 0 :(得分:0)
@ResponseBody
注释告诉控制器返回的对象会自动序列化为JSON
并传回HttpResponse
对象。
您应该删除@ResponseBody
@RequestMapping("/index")
public String index() {
return "index";
}
我能够通过上述更改成功运行您的代码,我没有遇到任何循环路径错误或404.
<强>证明强>
@Controller
public class HomeController {
@RequestMapping("/")
public String home() {
return "redirect:/index";
}
@RequestMapping("/index")
public String index() {
return "index";
}
}
017-12-11 21:15:07.835 INFO 4840 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-12-11 21:15:07.843 INFO 4840 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-12-11 21:15:07.843 INFO 4840 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-12-11 21:15:07.844 INFO 4840 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-12-11 21:15:07.844 INFO 4840 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-12-11 21:15:08.311 INFO 4840 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6c9f5c0d: startup date [Mon Dec 11 21:15:04 IST 2017]; root of context hierarchy
2017-12-11 21:15:08.409 INFO 4840 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/index]}" onto public java.lang.String com.userFront.controller.HomeController.index()
2017-12-11 21:15:08.410 INFO 4840 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.userFront.controller.HomeController.home()
2017-12-11 21:15:08.416 INFO 4840 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-12-11 21:15:08.416 INFO 4840 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-12-11 21:15:08.464 INFO 4840 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-11 21:15:08.464 INFO 4840 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-11 21:15:08.516 INFO 4840 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
请执行mvn clean install。希望能有所帮助
答案 1 :(得分:0)
我看着你最后的更改,看到你重定向到索引,但你没有映射&#34; / index&#34;。 也许你应该这样做
@RequestMapping("/")
public String home() {
return "redirect:/welcome";
}
@RequestMapping("/welcome")
public String index() {
return "index";
}
它会起作用,但看起来不是逻辑。 只需写下
@RequestMapping("/")
public String home() {
return "index";
}
这就是返回索引页面的全部内容。 确保你已将index.html页面设置为src / main / resources / static文件夹如果你没有更改默认配置,那么spring boot会看到那里