Question

I'm convinced I have something setup wrong somewhere but I can't figure out where as I have tried following multiple tutorials and solution (viralpatel.net, stackoverlow question, another stackoverflow question) but I cannot deploy a simple HelloWorld Spring MVC to tomcat.

I have tried multiple tutorials on multiple machines (windows 7 and os x 10.7), and I end up with the same issue. The main tutorial I was following was the mykong Spring 3 MVC hello World example , I even tried to download the supplied source code and still cannot get it to work ( it is becoming very Frustrating).

My Server is apache-tomcat-6.0.36 and it appears to deploy to tomcat correctly but I cannot get to any pages (I have even tried just putting a plain html page in the root but that still gives a 404 error) but if I manually create a new folder "test" in the webapps directory and put a index.html in there then localhost:8081/test/ works fine. I have also tried Tomcat 7 but made no difference.

This my project structure:

Project structure

this is my web.xml:

 <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 Web MVC Application</display-name>

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

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

This is my mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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.mkyong.common.controller" />

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

</beans>

This is the Controller:

package com.mkyong.common.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/welcome")
public class HelloController {

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        model.addAttribute("message", "Spring 3 MVC Hello World");
        return "hello";

    }

}

hello.jsp:

<html>
<body>
    <h1>Message : ${message}</h1>   
</body>
</html>

Solution

As duffymo pointed out, my application wasn't being deployed correctly, So I right clicked on the project in eclipse and exported as a "WAR" file to my tomcat webapps directory and it is working now

Was it helpful?

Solution

You are not packaging and deploying your app correctly.

I'd recommend that you package your app into a proper WAR file, put that in the /webapps folder, and start Tomcat.

If the package is named foo.war, your URL will be:

http://localhost:8080/foo/welcome

If your response is "I did that and it didn't work", the answer is "You still did it wrong."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top