Subhabrata Mondal
Subhabrata Mondal

Reputation: 536

Spring MVC 404 Not Found

I have started working with Spring MVC in Maven based project. My project directory structure is enter image description here

The content of web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <module-name>Admin Application</module-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servletContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

The content of servetContext is

<?xml version="1.0" encoding="UTF-8"?>
<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="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.web.controller"/>
    <bean name="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>/WEB-INF/config.properties</value>
            </list>
        </property>
    </bean>
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>
    <mvc:annotation-driven/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

Within the LoginController i have defined one method that will handle for initial HttpRequest

@Controller
public class LoginController {
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String signIn() {
        return "sign-in";
    }
}

When i request for the sign in page using http://localhost:8080/AdminApplication/

It gives me 404 not found error. Can you tell me what is the mistake i have done at here for the entire setup.

Upvotes: 0

Views: 1105

Answers (2)

Subhabrata Mondal
Subhabrata Mondal

Reputation: 536

I have done mistake one mistake in Parent POM.xml, where i have defined scope as provided for spring-mvc so that it is not packaged with in lib folder. That is why dispatcher class was not found. I have checked the log and solved the issue. Thank you for all.

Upvotes: 0

Kuldeep S Chauhan
Kuldeep S Chauhan

Reputation: 252

You should have a index page, placed directly under webapp folder, for every web-application which is the first page that is loaded when application is accessed.

Include this in your web.xml

 <welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>

redirect page(jsp) can be like :

  <%@page contentType="text/html" pageEncoding="UTF-8"%>
  <% response.sendRedirect("signin"); %>

Then you could map "signin" to spring's indexcontroller. Which will have your "sign-in" page as view

Upvotes: 1

Related Questions