2

I am creating a log in page, which will allow the user to sign in. However, when I try to visit http://localhost:8080/index via Google Chrome, I am just seeing "index," nothing else. But it is supposed to display the log in page that I created using HTML and Bootstrap.

Here are the codes of 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";
    }

}

You can find all the project files here: https://drive.google.com/open?id=1fxTWDo_3_iaS4zaav60KaT9OvB-kSjv6

Update:1 I have been advised to remove @ResponseBody from the method index(). But when I do that, the following problem occurs:

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.) 

Update 2: Here is the latest version of HomeController class, which I modify based on the advise I receive from @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";
        }

    }

But now, a new error occurs, which says:

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

Update 3: As advised by @vtx, I have tried to clean install Maven. However, the following error occurs:

[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

Update 4: As suggested by @vtx, I have performed clean installation of Maven.
I have followed stackoverflow.com/questions/19655184/… and stackoverflow.com/questions/19655184/…. Maven is installed cleanly without any error. But now, I am getting "Circular view path" error.

Update 5: @vtx suggested me to use the following code again:

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

    } 

But once again, I am getting the following error:

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

It's getting incredibly frustrating. But I will not give up!

john
  • 475
  • 2
  • 5
  • 19
  • 1
    use `"redirect:/index.html"` instead of `"redirect:/index"` – Youcef LAIDANI Dec 11 '17 at 11:58
  • Work through a good Spring Boot Web Starter Tutorial. You need a view definition to be stored in the correct location - e.g. a Thymeleaf file or a Freemarker Template. If there is no "index.ftl" or similar (in the correct location), Spring Boot does not know what to display as "index", and just invokes the controller again. Also, you should show your SecurityConfigurator class, which you must use to register your form login page. – Florian Albrecht Dec 11 '17 at 12:13
  • @ Florian Albrecht : spring-boot-starter-thymeleaf dependency is available in pom and index.html is available in resource-templates location..so the viewresolver is capable of finding index.html file if we return simply index – Vishnu T S Dec 11 '17 at 12:20
  • @YCF_L Still doesn't work. Showing the same thing. – john Dec 11 '17 at 12:33
  • You don't have a mapping for /index. Change `@RequestMapping("/welcome")` to `@RequestMapping("/index")`. – Christian Dec 11 '17 at 18:38
  • @Christian After changing that, I am getting "circular view path" error. 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.) – john Dec 11 '17 at 18:42

2 Answers2

0

The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object.

You should remove @ResponseBody

@RequestMapping("/index")
    public String index() {
        return "index";
    }

I was able to run your code successfully with the changes mentioned above, I didn't face any circular path error or 404.

Proof

@Controller
public class HomeController {

    @RequestMapping("/")
    public String home() {
        return "redirect:/index";
    }

    @RequestMapping("/index")
    public  String index() {
        return "index";
    }

}

enter image description here

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]

Please do mvn clean install. Hope that will help

Vishnu T S
  • 3,476
  • 2
  • 23
  • 39
  • @john exactly ...try localhost:8080/welcome or localhost:8080/userFront/welcome on chrome – Vishnu T S Dec 11 '17 at 12:50
  • Can you update the question with latest HomeController class – Vishnu T S Dec 11 '17 at 12:54
  • @RequestMapping("/index") and index.html are different. so you can change it. in updated version you dont have any path named /index so I advised you to change redirect:/index to redirect:/welcome. it wont resolve your issue. because you are invoking localhost:8080/UserFront/welcome. It is mapped to @RequestMapping("/welcome") – Vishnu T S Dec 11 '17 at 13:50
  • @john , I was able to run your code by simply removing the Responsebody. I didn't change anything else. – Vishnu T S Dec 11 '17 at 15:48
  • Please do mvn clean install. hope it will help. because its worked for me without any issues – Vishnu T S Dec 11 '17 at 15:52
  • How can I clean install the maven? I have followed this video, but it doesn't work. https://www.youtube.com/watch?v=cR9sqUrJwBU – john Dec 11 '17 at 17:12
  • right click on project and run as maven clean and then run as maven install..follow this link https://stackoverflow.com/questions/19793895/run-mvn-clean-install-in-eclipse – Vishnu T S Dec 11 '17 at 17:25
  • I used maven clean and maven install options. But it looks like it is not being installed correctly. I am going to post the errors in the updated description. – john Dec 11 '17 at 17:33
  • Follow this link https://stackoverflow.com/questions/19655184/no-compiler-is-provided-in-this-environment-perhaps-you-are-running-on-a-jre-ra – Vishnu T S Dec 11 '17 at 17:38
  • I have followed https://stackoverflow.com/questions/19655184/no-compiler-is-provided-in-this-environment-perhaps-you-are-running-on-a-jre-ra and https://stackoverflow.com/questions/19655184/no-compiler-is-provided-in-this-environment-perhaps-you-are-running-on-a-jre-ra. Maven is installed cleanly without any error. But now, I am getting "Circular view path" error. It's incredibly frustrating. – john Dec 11 '17 at 18:12
  • Expand maven dependency view form project exploror and check for thymeleaf decency. – Vishnu T S Dec 11 '17 at 18:16
  • There are multiple thymeleaf dependencies, including thymeleaf-spring-2.1.6.RELEASE.jar and thymeleaf-2.1.6.RELEASE.jar. Which one are you talking about? – john Dec 11 '17 at 18:22
  • All of them are needed. just want to check maven worked fine. Now try to change @RequestMapping("/index") to @RequestMapping("/welcome") and invoke localhost:8080/welcome – Vishnu T S Dec 11 '17 at 18:24
  • 404 issue has occurred again. There was an unexpected error (type=Not Found, status=404). No message available – john Dec 11 '17 at 18:29
  • I have tried all the way. its working for me, if you have any remote access tool I will be able to help you further – Vishnu T S Dec 11 '17 at 18:31
  • 1
    I am incredibly grateful to you. I never expected someone to help me in possibly all the ways. God bless you. It's mid night here. So,I need to go to bed now. I will come up with a remote access tool tomorrow. – john Dec 11 '17 at 18:36
  • Would you please tell me which remote software should I use? Which one do you have? – john Dec 12 '17 at 06:45
0

I look at you last changes and see that you redirect to index but you don't have mapping for "/index". Maybe you should do this

 @RequestMapping("/")
    public String home() {
        return "redirect:/welcome";
    }

    @RequestMapping("/welcome")
    public String index() {
        return "index";
    }

It will work but seemes not logic. Just write

@RequestMapping("/")
    public String home() {
        return "index";
    }

That's all to return you index page. Make sure you have set you index.html page to src/main/resources/static folder If you didn't change default configurations spring boot will look there

Den B
  • 843
  • 1
  • 10
  • 26