Home > Software engineering >  Why Spring wont run right path automatic
Why Spring wont run right path automatic

Time:10-22

I have this controller:

@Controller
@RequestMapping("/home")
public class Home {

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

}

When I run program I got this URL: http://localhost:8080/E-CommerceFinal-1.0-SNAPSHOT/ and its 404 not found, but when I add /home/showHome its works. In such simple projects, earlier when I start the program, it automatically throws out the required address

CodePudding user response:

If you want http://localhost:8080/E-CommerceFinal-1.0-SNAPSHOT/ to be a valid URL returning the index page you need to change your controller as follows:

@Controller
public class Home {

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

With your current code (@RequestMapping("/home") and @RequestMapping("/showHome")), you are telling Spring that the index page will be available at http://localhost:8080/E-CommerceFinal-1.0-SNAPSHOT/home/showHome as you wrote.

CodePudding user response:

Please don't cross post -- you've already gotten this answered (by the same people) here Spring controller doesn't work with mapping

  • Related