-2

I'm pretty new to Spring, and encountering a problem with RequestMapping. I want to learn using annotations, so i tried creating a small project which works with those. Anyway, i keep getting 404, but as far as i understand i did everything right. Would be really happy about some help, couldnt find a solution while reading several tutorials, which do basically the same. Thanks!

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

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


    <context:component-scan base-package="com.controller" />
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>


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


</beans>

The Controller "MainController.java" in package com.controller:

package com.controller;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
/**
 *
 * @author 
 */

@Controller
public class MainController{

    @RequestMapping("/main")
    public ModelAndView hithere() {

        ModelAndView mav = new ModelAndView ("main");
        mav.addObject("welcomeMessage", "Hi there");

        return mav;
    }
}

And then there is of course a main.jsp in the Web-inf/jsp folder.

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
Heady
  • 935
  • 3
  • 10
  • 23

2 Answers2

0

Either change the url mapping of your dispatcher servlet to :

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

Or change your RequestMapping(value="/main") to RequestMapping(value="/main.htm")

Rafik BELDI
  • 4,140
  • 4
  • 24
  • 38
0

Oh well found it..

So this was missing in the web.xml:

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

And this

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

Was moved from dispatcher to applikation.xml. Didn't look out for that because netbeans generated it that way and i trusted it :D

Heady
  • 935
  • 3
  • 10
  • 23