randmw
randmw

Reputation: 23

Basic Spring 4 MVC - Hello World - Error 404 File Not Found

I am trying to access localhost:8080/HelloWeb/helloWorld, but keep getting a file not found error. I know that I'm not doing the mapping correctly, but as a beginner, I can't really pinpoint where. Please help.

I have the following view in WEB-INF/jsp/helloWorld.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Spring MVC Web Service</h1>
        <h3>Name: ${name}</h3>
    </body>
</html>

The following is my web.xml configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>    
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>

The following is my dispatcher-servlet.xml file:

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"       
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

    <mvc:annotation-driven/>
    <context:component-scan base-package="controller" />

</beans>

And the following is my HelloWorldController.java file:

package controller;

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

@Controller
public class HelloWorldController {
    @RequestMapping("/helloWorld")
    public String helloWorld (Model model) {
        model.addAttribute("message", "Hello World from Controller");
        return "helloWorld";
    }
}

Apologize if the question has been asked previously but I don't seem to be able to resolve the problem from answers that I have seen so far. Thanks again.

Upvotes: 2

Views: 1859

Answers (3)

morsor
morsor

Reputation: 1313

Remove the urlMapping and the configuration of the indexController.

Since you are doing a component scan, your HelloWorldController should be picked up. This can be verified in the log messages when you start the container.

Your controller returns 'helloWorld' which using the viewResolver should pick up your JSP.

Upvotes: 0

Cortwave
Cortwave

Reputation: 4907

Yous dispatcherServlet processes only url by pattern *.htm. Add *.htm suffix to your url in @RequestMapping:

@RequestMapping("/helloWorld.htm")

Upvotes: 1

sdwaraki
sdwaraki

Reputation: 412

If you are using .htm* regular expression in your html, I think you should go for localhost:8080/HelloWeb/helloWorld.html or you should change the servlet mapping in web.xml to / instead of .htm*

Also, if you are adding attribute message to the model in the controller, make sure you use the same attribute in the JSP too. So it should be

Name: ${message}

Upvotes: 0

Related Questions