Stepan Mocanu
Stepan Mocanu

Reputation: 69

Spring MVC: Tomcat 404 The requested resource is not available

i am new in Spring framework and working on HelloSpring project and I am still getting the HTTP Status 404 error. I am pretty desperate right now. May anyone tell me, what is the problem, please?

web.xml

<?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>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.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>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</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>

dispatcher-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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"
   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"
   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"/>

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

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

IndexController.java

@Controller
public class IndexController{
@RequestMapping("welcome")
public String index(ModelMap map, HttpServletRequest r){
    System.out.println("Kontrola do konzole");
    map.put("msg", "This is index page");
    return "index";
}
}

redirect.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%><% response.sendRedirect("welcome"); %>

Tomcat Log

05-Feb-2017 14:14:00.583 WARNING [http-nio-8084-exec-495] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/MavenHello/welcome] in DispatcherServlet with name 'dispatcher'

When i Run the application, I always get 404 Error on URL http://localhost:8084/MavenHello/welcome

Upvotes: 1

Views: 3157

Answers (1)

Mouad EL Fakir
Mouad EL Fakir

Reputation: 3759

So let's start it all over again :

1# Add this snippet in your web.xml (get rid of redirect.jsp we don't need it anymore)

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

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

    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>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/applicationContext.xml</param-value>
</context-param>

2# In dispatcher-servlet.xml put this snippet (get raid of all others configs) :
Be sure to change xx.yy.zz.controllers with your real package ( where your are putting controllers.)

<context:component-scan base-package="xx.yy.zz.controllers" />
<mvc:annotation-driven />

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

3# Make sure your IndexController look like this:

package xx.yy.zz.controllers;

//imports here...

@Controller
public class IndexController {

    @RequestMapping(value={"/", "/index", "/welcome"}, method = RequestMethod.GET)
    public String index(Map<String, Object> model) {

        System.out.println("Kontrola do konzole");

        map.put("msg", "This is index page");

        //it will be redirected to /WEB-INF/jsp/index.jsp (make sure that have this page)
        return "index";
    }
}

4# And finally add a /WEB-INF/jsp/index.jsp

<html>
<body>
    <c:if test="${not empty msg}">
        Hello ${msg}
    </c:if>
</body>
</html>

That's it, now you can test your app using one of the following urls:

Let me know if sth goes wrong, good luck.

Upvotes: 1

Related Questions