-1

I'm trying to make a simple web application which has a login and a welcome page using Spring MVC. The code is as follows:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Spring MVC Application</display-name>

   <servlet>
      <servlet-name>spring</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
   </servlet>

   <servlet-mapping>
      <servlet-name>spring</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

</web-app>

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"    
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
    xmlns:p="http://www.springframework.org/schema/p"    
    xmlns:context="http://www.springframework.org/schema/context"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
http://www.springframework.org/schema/context    
http://www.springframework.org/schema/context/spring-context-3.0.xsd">    

<context:component-scan base-package="com.test"></context:component-scan>  

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
<property name="prefix" value="/WEB-INF/jsp/"></property>  
<property name="suffix" value=".jsp"></property>  
</bean>  

</beans> 

Controller.java

@Controller
@RequestMapping("/Authentication")
public class TestController{
    @RequestMapping(value="/")
    public String Login(){
        return "Login";
    }

    @RequestMapping(value="Authenticate", method=RequestMethod.GET)
    public String Authenticate(){
        //Authenticates and returns "Welcome"
    }
} 

The project name is Authentication. There are Login.jsp and Welcome.jsp under /WEB-INF/jsp/.

However, when I'm trying to run the project, I am getting HTTP Status 404 error and the following warning:

org.springframework.web.servlet.PageNotFound noHandlerFound WARNING : No mapping found for http request with uri [/Authentication/] in dispatcherservlet with name 'spring'

Why am I getting this warning even though the mappings look fine?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
wackyTechie
  • 115
  • 1
  • 1
  • 8
  • 1
    [Welcome, number 338](http://stackoverflow.com/search?q=is%3Aq+%22No+mapping+found+for+http+request+with+uri%22+%22in+dispatcherservlet+with+name%22). – BalusC Sep 29 '16 at 22:21
  • which url are you using to access your application? and which is the package of TestController class? – amicoderozer Sep 30 '16 at 08:01

1 Answers1

-1

You are not specifying where the spring-servlet will be located. Add these to your web.xml file:

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring-servlet.xml
    </param-value>
</context-param>
sirandy
  • 1,766
  • 4
  • 24
  • 29
  • Their servlet is named `spring`. Spring assumes a context file exists at `/WEB-INF/-servlet.xml` for that servlet's context. Why are you introducing a `ContextLoaderListener` here? – Sotirios Delimanolis Sep 29 '16 at 23:10
  • From the documentation: ContextLoaderListener supports injecting the root web application context via the ContextLoaderListener(WebApplicationContext) constructor, allowing for programmatic configuration in Servlet 3.0+ environments. Also @BalusC you could refer to [here](http://stackoverflow.com/questions/3652090/difference-between-applicationcontext-xml-and-spring-servlet-xml-in-spring-frame) – sirandy Sep 30 '16 at 02:54
  • This doesn't explain why it's needed. The `DispatcherServlet`'s context is quite enough in this case. – Sotirios Delimanolis Sep 30 '16 at 04:23
  • @Sotirios: I see you're into Spring. Could you curate a canonical duplicate for all those 338 questions? Here's an example: http://stackoverflow.com/q/30128395 Before I posted that and cleaned up all those duplicates, users kept posting the same question amost daily. – BalusC Sep 30 '16 at 21:09
  • @BalusC Sounds like a plan. After the weekend. – Sotirios Delimanolis Sep 30 '16 at 23:51