Question

I've been trying to figure out something which has been vexing me for a the whole day. I'm trying to run a very simple SpringMVC but the webapp doesn't seem to pick up any of the controllers i've set up. Any help would be great

Web.xml

<web-app 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">

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

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

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

</web-app>

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-4.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="com.thehit.*" />

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

application-context.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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" 
    default-autowire="byType">

    <context:annotation-config/>
    <context:component-scan base-package="com.thehit.*" />

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3307/thehit"/>
        <property name="username" value="root"/>
        <property name="password" value="Password01"/>      
    </bean> 

</beans>

HelloWorldController.java

package com.thehit.controllers;
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 HelloWorldController {

    @RequestMapping(value="/hello", method = RequestMethod.GET) 
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Spring 3 MVC Hello World. This is HelloControllerClass.");
        return "hello"; 
    } 
}

The jsp file is located in WEB-INF/pages

When I navigate to the following addresses "http://localhost:8080/TheHitWebProject/welcome" or "http://localhost:8080/TheHitWebProject/hello" I get the following error:

HTTP Status 404 -

type Status report

message

description The requested resource is not available.

Apache Tomcat/7.0.53

I know i am missing something basic here but i've been at it for hours and can't figure it out. Any help will be really appreciated

Thanks

Was it helpful?

Solution

You're missing mvc:annotation-driven:

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

    <mvc:annotation-driven/>

</beans>

Also remember that @RequestMapping from @Controller is added before @RequestMapping from method so url will be:

http://localhost:8080/TheHitWebProject/welcome/hello
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top