samar
samar

Reputation: 39

404 error in Spring MVC application

I have created a simple spring MVC application without using any build tools like maven or ant in eclipse EE IDE

The application consist of only one controller class, 2 XML's (web.xml and spring-servlet.xml) and a jsp page(hellopage.jsp)

I am using tomcat and 6.0 eclipse galileo.

In my spring-servlet.xml file I have mentioned <servlet-name> as "Spring" and <servlet-class> as org.springframework.web.servlet.DispatcherServlet, <url-pattern> as *.html, whereas in my web.xml file welcome file is index.jsp which has a link(<a href="hello.html">click</a>) to hello.html.

My controller class has request mapping as ("/hello"), when in the browser after deploying the war file of my application I hit the URL localhost:8080/projectname the index.jsp page pops up with a link "click", but after clicking on this link I get a 404 error mentioning "servlet spring is not available" means the dispatcher servlet which I mentioned in spring-servlet.xml file, please can anyone help?

Here is the code:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    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_2_5.xsd">
    <display-name>SpringMVC</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

HelloWorldController.java:

package com.samar.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {

    @RequestMapping("/hello")   
    public ModelAndView helloworld()
    {
        String message ="Hello spring MVC...!!";

        return new ModelAndView("hellopage","message",message);
    }

}

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.samar.controllers"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>
    <a href="/hello.html">Click</a>
</body>
</html>

hellopage.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>
    Message is ${message}
</body>
</html>

Upvotes: 0

Views: 1096

Answers (3)

Vinod
Vinod

Reputation: 4189

You have to tell Spring to read your @Controller and other annotations.

This can be done by adding

<context:annotation-config/>

to your spring-servlet.xml

Upvotes: 1

samar
samar

Reputation: 39

thanks guys,finally issue resolved actually the problem was apart from adding "spring core" and "spring web" jars in build path of the project i have to add them in the lib folder under WEB-INF as well, and it works now , that's why Tomcat was not able to find dispatcher servlet hence the 404 error.. i hope the answer helps beginners like me..

Upvotes: 1

Mecon
Mecon

Reputation: 997

"servlet spring not available" means your app didn't start up correctly.

Check your tomcat log for details.

This may have info you need: http://forum.spring.io/forum/spring-projects/web/41340-beginner-s-problem-servlet-is-not-available

Upvotes: 0

Related Questions