0

After setting up the default security page with Spring security (in memory), I receive the following error:

No mapping found for HTTP request with URI [/myproject/] in DispatcherServlet with name 'appServlet'

Update: This problem was fixed by moving the component-scan (which scans my security package) from the servlet-context.xml to root-context.xml

Moody
  • 851
  • 2
  • 9
  • 23
  • Please share the complete controller class. – Mithun Apr 12 '15 at 12:58
  • Thanks for your comment. I updated it with the full home controller, test controller as well as a angularjs app file (which contains some routing). (However I don't think it has anything to do with the angularjs app file) – Moody Apr 12 '15 at 13:13
  • What IDE are you using? – tmarwen Apr 12 '15 at 13:26

1 Answers1

1

You error messaege tells No mapping found for HTTP request with URI [/addressbook/]. In other words, you have not mapped a request mapping for the URL /addressbook

If you want your project to have a base URL as /addressbook then change the web.xml as below

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    ....
</servlet>
<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/addressbook/*</url-pattern>
</servlet-mapping>

Controller

@Controller
public class HomeController {
    @RequestMapping(value="/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        return "home";
    }
}

Then goto the browser and type the below. There should be a / at end.

http://hostname:port/addressbook/

But if you want to change the context root, you cannot do this by web.xml. Context root isn't part of the standard web.xml file. You can change this by doing the following in the eclipse

  1. In project properties click on Web Project Settings (assuming you are in a dynamic web project.) The only configuration value there is "Context root:"
  2. Change "Context root" to /addressbook
  3. Change the web.xml url-pattern to <url-pattern>/</url-pattern>
  4. rebuild
  5. remove the project from tomcat
  6. redeploy.

Quoted from digitaljoel

Community
  • 1
  • 1
Faraj Farook
  • 14,385
  • 16
  • 71
  • 97
  • Thanks for your comment. That does not work. I still receive the same error. – Moody Apr 12 '15 at 13:25
  • I did some changes to the web.xml. Can you check it again. You have to access the page as `http://:/myproject` – Faraj Farook Apr 12 '15 at 13:34
  • After your edit, I don't receive the error anymore but I do receive a: HTTP Status 404 - /addressbook/ error when I access the page. – Moody Apr 12 '15 at 13:35
  • there is no request mapping configured for the `/addressbook` in your controllers. thats the reason for 404. – Faraj Farook Apr 12 '15 at 13:39
  • Hi, I thought that as well (I'm using addressbook instead of myproject sorry for the confusion, but I changed my web.xml according to your code above with addressbook). I tried doing: @RequestMapping(value="/addressbook",method = RequestMethod.GET) for instance but that still does not work. I tried /addressbook/, addressbook/ and it still does not work. Very weird. Same error again, HTTP Status 404 - /addressbook/. – Moody Apr 12 '15 at 13:45
  • your URL base should be `addressbook ` or `myproject`. According to that edit the web.xml and in the url pattern add `/myproject/*` or /addressbook /* . Then DO NOT remap `@RequestMapping(value="/addressbook"`. This will make your complete URL look like `/myproject/addressbook` or `/addressbook/addressbook` – Faraj Farook Apr 12 '15 at 13:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75056/discussion-between-faraj-farook-and-justlearning). – Faraj Farook Apr 12 '15 at 13:50
  • Thanks mate I posted there as well and updated the main post here with what I have now. – Moody Apr 12 '15 at 14:36
  • You'r wecome. I updated the answer. Can you check it now. Try with a clean build. – Faraj Farook Apr 12 '15 at 14:53
  • I tried the same thing (same code as you) with a clean build. I browsed to http://localhost:8080/addressbook/ and it showed the login page (url changed to http://localhost:8080/addressbook/login). After logging in, the url changes to http://localhost:8080/addressbook/ and I still receive a HTTP Status 404 - /addressbook/ error. I truly don't understand why this is happening. – Moody Apr 12 '15 at 15:00
  • Can you post your spring config? Is `/addressbook/error` the error page for login failure? – minion Apr 12 '15 at 15:15
  • @JustLearning To make the Base URL redirect to /addressbook you have to make changes in the context.xml in tomcat. Context root isn't part of the standard web.xml file. So you having a URL mapped to the `addressbook ` will not make you access the url `localhost:8080/addressbook` but rather it should be `localhost:8080/addressbook/` – Faraj Farook Apr 12 '15 at 15:24
  • @minion, which file do you mean exactly? I posted both web.xml and root-context, servlet-context in the main post. :) – Moody Apr 12 '15 at 15:47
  • @FarajFarook , Regardless whether I access localhost:8080/addressbook/ or localhost:8080/addressbook, during the first time, I see the login page, but after I login , I receive the error. (The login redirects me to the HTTP Status 404 - /addressbook/ error) The base url, it doesn't really matter for me. If it is localhost:8080/adressbook, it is fine. The problem I have is, I don't understand why it is redirecting me, after logging in, to a HTTP Status 404 – Moody Apr 12 '15 at 15:48
  • @FarajFarook, I see your updated post. I tried it, but still, after logging in, I receive a HTTP Status 404. I have a feeling this is caused because I have two files: root-context.xml and servlet-context.xml. – Moody Apr 12 '15 at 15:55
  • I FIXED THE PROBLEM! I put in root-context.xml. Then I put both in servlet-context.xml, and it worked!!!!!!!! :) Thank you very much for your efforts guys! I appreciate it. But does anyone understand WHY this is caused like that? – Moody Apr 12 '15 at 16:14