0

I'm beginner in Spring MVC

I try to show "hello world" il my browser

But my browser show HTTP 404 -

My Project structure is :

SpringMVC
     -> src
            -> main
                    -> java
                          -> com
                                -> mkyong
                                         -> commmon
                                                  -> controller


    -> webapp
            -> WEB-INF
                     -> pages

I have HelloWorldController.java in folder controller :

 package com.mkyong.common.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloWorldController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        ModelAndView model = new ModelAndView("HelloWorldPage");
        model.addObject("msg", "hello world");

        return model;
    }

}

in pages I have HelloWorldPage.jsp :

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
    <h1>Spring MVC Hello World Example</h1>

    <h2>${msg}</h2>

</body>
</html>

mvc-dispatcher-servlet.xml in WEB-INF :

<beans xmlns="http://www.springframework.org/schema/beans"
    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-2.5.xsd">

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

    <bean name="/welcome.htm" 
        class="com.mkyong.common.controller.HelloWorldController" />

</beans>

And finaly web.xml in WEB-INF :

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

2 Answers2

0

In your mvc-dispatcher-servlet.xml file add the following line

<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

However I would recommend using the newer way of using annotations for creating MVC applications. It is much cleaner.

See this link http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-ann-controller

Ayub Malik
  • 2,488
  • 6
  • 27
  • 42
0

Actually in another article the author of the example says, "Actually, declare BeanNameUrlHandlerMapping is optional, by default, if Spring can’t found handler mapping, the DispatcherServlet will creates a BeanNameUrlHandlerMapping automatically."

user2319284, I think your problem might be that the URL pattern you have declared in your web.xml is url-pattern>/, but I think it needs to be .htm so when you enter the browser URL the "welcome.htm” pattern is mapped to your HelloWorldController class.

Try using this web.xml (from the same site as your example) instead:

    <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>*.htm</url-pattern>
  </servlet-mapping>

</web-app>

And use URL http://localhost:8080/SpringMVC/welcome.htm
wSchmidt
  • 100
  • 5