0

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>
3
  • 5
    So much easier for everyone to debug (including yourself) if you post the code Commented Apr 29, 2015 at 20:57
  • Welcome to StackOverflow! Please be sure to read our How to Ask page to help you formulate a great question. You are much more likely to get a good answer from the community if you put some effort into your question. Commented Apr 29, 2015 at 21:01
  • 1
    From just looking at your html you've posted it may be that the hello.html file cannot be found because you need to give it the fully qualified resource location, i.e like this href="${pageContext.request.contextPath}/hello.html" Commented Apr 29, 2015 at 21:02

3 Answers 3

1

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..

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

"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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.