0

I'm getting the 404 error with my first spring mvc application, followed all the configurations but no luck. Can you please help to figure out the problem.

    Following is the change:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>HereWeGo</display-name>


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

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


</web-app>

spring-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.1.xsd
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

    <mvc:annotation-driven />

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

</beans>

HeyThere.java

package com.aditya.spring;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.config.annotation.EnableWebMvc;


@EnableWebMvc

@Controller

public class HeyThere{

      @RequestMapping("/welcome")
      protected ModelAndView handleRequestInternal(){
          ModelAndView modelandview = new ModelAndView("HelloPage");

          modelandview.addObject("welcomeMessage","Hi user, welcome to the first spring mvc application");

          return modelandview;

      }

}

Hello.jsp

<html>
<body>
    <h1>First Spring MVC Application Demo</h1>
    <h2>${welcomeMessage}</h2>
</body>
</html>

In the server console i'm getting below warning:

org.springframework.web.servlet.PageNotFound noHandlerFound

WARNING: No mapping found for HTTP request with URI [/HereWeGo/] in DispatcherServlet with name 'spring-dispatcher'

My project name is HereWeGo.

ADITYA VALLURU
  • 111
  • 1
  • 3
  • 10

1 Answers1

0

Your configuration looks ok, except the @EnableWebMvc annotation (which is specific to Java configurations) which does the same as your <mvc:annotation-driven /> tag.(Reference here EnableWebMvc annotation meaning)

Also the fix for this issue, as stated in the comments should be to add <context:component-scan base-package="com.aditya.spring" /> the spring-dispatcher-servlet.xml after the <mvc:annotation-driven> tag for registering the controller in the spring application context.

As per your exception, your controller is mapped to /welcome, so you should try localhost:<YourServerPort>/<YourWebAppDeployName>/welcome to entry your controller method and acces your jsp-view.

Community
  • 1
  • 1
Alex Ciocan
  • 2,272
  • 14
  • 20
  • Removed specified annotation and accessed with "/welcome", getting same response with following error. **org.springframework.web.servlet.PageNotFound noHandlerFound** **WARNING: No mapping found for HTTP request with URI [/HereWeGo/welcome] in DispatcherServlet with name 'spring-dispatcher'** – ADITYA VALLURU Dec 02 '16 at 11:56
  • Please try localhost:8080/com.aditya.spring/welcome if 8080 is your server port and let me know if its working – Alex Ciocan Dec 02 '16 at 12:00
  • No luck, one more observation is : is also giving 404 page. – ADITYA VALLURU Dec 02 '16 at 12:05
  • Did your try adding to register your controller bean in the spring context. – Alex Ciocan Dec 02 '16 at 12:18
  • By adding `` in your spring-dispatcher-servlet.xml after the `` tag – Alex Ciocan Dec 02 '16 at 12:24
  • Yeah now its working !! I've a query : where we are specifying the jsp file name, how it will detect to which file to take – ADITYA VALLURU Dec 02 '16 at 13:12
  • The InternalResourceViewResolver is responsible for this issue. You specify your view via your ModelAndView object from your controller(either by constructor as you implemented it, either by setView). – Alex Ciocan Dec 02 '16 at 13:26