Java Spring启动银行项目:未显示登录页面

Java Spring启动银行项目:未显示登录页面,java,spring,spring-boot,Java,Spring,Spring Boot,我正在创建一个登录页面,允许用户登录。然而,当我试图通过谷歌浏览器访问时,我看到的只是“索引”,其他什么都没有。但它应该显示我使用HTML和引导创建的登录页面 以下是HomeController.java的代码 package com.userFront.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMappi

我正在创建一个登录页面,允许用户登录。然而,当我试图通过谷歌浏览器访问时,我看到的只是“索引”,其他什么都没有。但它应该显示我使用HTML和引导创建的登录页面

以下是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";
    }

}
您可以在此处找到所有项目文件:

更新: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

这令人难以置信地沮丧。但我不会放弃

注释
@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信息4840---[ost-startStop-1]o.s.b.w.servlet.ServletRegistrationBean:将servlet:'dispatcherServlet'映射到[/]
2017-12-11 21:15:07.843信息4840---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:将筛选器:“characterEncodingFilter”映射到:[/*]
2017-12-11 21:15:07.843信息4840---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:将筛选器:“hiddenHttpMethodFilter”映射到:[/*]
2017-12-11 21:15:07.844信息4840---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:将筛选器:“httpPutFormContentFilter”映射到:[/*]
2017-12-11 21:15:07.844信息4840---[ost-startStop-1]o.s.b.w.servlet.FilterRegistrationBean:将筛选器:“requestContextFilter”映射到:[/*]
2017-12-11 21:15:08.311信息4840---[main]s.w.s.m.a.RequestMappingHandlerAdapter:正在寻找@ControllerAdvice:org.springframework.boot.context.embedded。AnnotationConfigEmbeddedWebApplicationContext@6c9f5c0d:启动日期[2017年12月11日星期一21:15:04 IST];上下文层次结构的根
2017-12-11 21:15:08.409信息4840---[main]s.w.s.m.m.a.RequestMappingHandlerMapping:将“{[/index]}”映射到公共java.lang.String.com.userFront.controller.HomeController.index()上
2017-12-11 21:15:08.410信息4840---[main]s.w.s.m.m.a.RequestMappingHandlerMapping:将“{[/]}”映射到公共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:将“{[/error]}”映射到public org.springframework.http.ResponseEntity 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.a.RequestMappingHandlerMapping:将“{[/error],products=[text/html]}”映射到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信息4840---[main]o.s.w.s.handler.simplerlhandler映射:将URL路径[/webjars/**]映射到[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]类型的处理程序上
2017-12-11 21:15:08.464信息4840---[main]o.s.w.s.handler.simplerlhandlermapping:将URL路径[/**]映射到[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]类型的处理程序上
2017-12-11 21:15:08.516信息4840---[main]o.s.w.s.handler.simplerlhandler映射:将URL路径[/**/favicon.ico]映射到[class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]类型的处理程序上

请执行mvn清洁安装。希望这会有所帮助

我查看了您最近的更改,发现您重定向到了索引,但您没有“/index”的映射。 也许你应该这样做

 @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将在那里查看

使用
“redirect:/index.html”
而不是
“redirect:/index”
学习一个好的spring boot Web初学者教程。您需要将视图定义存储在正确的位置,例如Thymeleaf文件或Freemarker模板。如果没有“index.ftl”或类似内容(在正确的位置),Spring Boot不知道显示什么作为“index”,只需再次调用控制器。另外,您应该显示SecurityConfiguration类,您必须使用它来注册表单登录页。@Florian Albrecht:spring boot starter thymeleaf依赖项在pom中可用,index.html在资源模板位置中可用。因此,如果我们只返回index.html,viewresolver就能够找到index.html文件index@YCF_L还是不行。显示相同的内容。您没有/索引的映射。将
@RequestMapping(“/welcome”)
更改为
@RequestMapping(“/index”)
@john…尝试localhost:8080/welcome或localhost:8080/u
@RequestMapping("/")
    public String home() {
        return "index";
    }