Mr Asker
Mr Asker

Reputation: 2380

HTTP Status 404 - /FirstSpringMVCProject/WEB-INFHelloPage/.jsp

I am trying to learn the spring mvc framework but when I try to run the code underneat on the server, I am not getting anything rendered also only the error in the screen shoot. But I have noticed that the url has the following WEB-INFHelloPage/.jsp in the 404 error. Is that normal (also wihout backslash between WEB-INF/HelloPage.jsp)? And Why am I not getting something as:

First Spring MVC Application Demo

Hi user, welcome to the first Spring MVC Application

rendered?

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>FirstSpringMVCProject</display-name>

    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    </servlet>

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


</web-app>

HelloController class:

package com.stack;

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 HelloController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ModelAndView modelandview = new ModelAndView("HelloPage");
        modelandview.addObject("welcomeMessage", "Hi User, welcome to the first Spring MVC Application");
        return modelandview;
    }

}

spring-dispatcher-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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

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

    <bean name="/welcome.html" class="com.stack.HelloController" />

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


    </bean>


</beans>

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>
<!-- <title>Insert title here</title> -->
</head>

<h1> First Spring MVC Application Demo </h1>

<h2>${welcomeMessage}</h2>

<body>

</body>
</html>

screen shoot

enter image description here

Upvotes: 0

Views: 160

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148880

The error is explicit about that: you forgot the ending / in prefix property. It should be:

...
<property name="prefix">
            <value>/WEB-INF/</value>
        </property>
...

But with a decent version of Spring MVC, you have no reason to use an explicit BeanNameUrlHandlerMapping nor derive you controller class from AbstractController. Those were for Spring 2.0, but since 3.1 RequestMapping annotated methods are both more powerful and simpler to use.

Upvotes: 1

Related Questions