0

I am new to working with Spring and decided to follow this tutorial:

http://www.programcreek.com/2014/02/spring-mvc-helloworld-using-maven-in-eclipse/

My files and file structure match the tutorial, and index.jsp is working. However, when I click to go to helloworld.jsp, I get the following 404 error:

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Can anybody suggest places to dig? Is there something wrong with the tutorial that is not suited to Tomcat 8.5? Or is it more likely that there is something wrong with my setup?

EDIT: I have the following installed:

  1. Tomcat 8.5.14
  2. Eclipse Neon with Spring IDE
  3. Maven 3.5.0

If it helps, Maven has been working before I tried using it with a Web/Spring (ie mvn install downloads the correct libraries)

I have included an image of my files and a below is the actual code of the files that I believe are relevant:

UserController.java

package com.ankurmgoyal.hellotest.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {


String message = "Welcome to Spring MVC!";

    @RequestMapping("/hello")
    public ModelAndView showMessage(
            @RequestParam(value = "name", required = false, defaultValue = "World") String name) {
        System.out.println("in controller");

        ModelAndView mv = new ModelAndView("helloworld");
        mv.addObject("message", message);
        mv.addObject("name", name);
        return mv;
    }
}

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>Archetype Created Web Application</display-name>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <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>
</web-app>

index.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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC - HelloWorld Index Page</title>
</head>
<body>

    <center>
        <h2>Hello World</h2>
        <h3>
            <a href="hello">Click Here</a>
        </h3>
    </center>
</body>
</html>

helloworld.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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC -HelloWorld</title>
</head>
<body>
    <center>
        <h2>Hello World</h2>
    </center>
</body>
</html>

dispatcher-servlet.xml

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

    <context:component-scan base-package="com.ankurmgoyal.hellotest.controller" />

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

1 Answers1

0

index.jsp is served by DefaultServlet. The url-pattern for dispatcher is /. / pattern overrides the defaultServlet and all the calls are routed through this servlet. Change the url pattern to some non-empty string and try it out.

Refer to this SO Question for more details.

Community
  • 1
  • 1
yaswanth
  • 2,349
  • 1
  • 23
  • 33
  • Thanks so much for the response. I looked a little deeper and tried the different url patterns suggested in the link you sent, but when trying those, I got the same error except now on index. Do you believe that essentially my problem is that when I click on "Click Here" on index, the "/hello" is not being sent to the DispatcherServlet? – Ankur Goyal May 12 '17 at 17:15
  • Can you post the exact code you are using? That will be easier to understand – yaswanth May 12 '17 at 17:41
  • Sure thing - added the files. Thanks again for taking the time to help. – Ankur Goyal May 12 '17 at 17:59
  • I have tried the exact setup but on intellij with tomcat7 maven plugin. It seems to work fine for me. What happens if you hit localhost/hellotest/hello? Are you able to access helloworld.jsp now? – yaswanth May 13 '17 at 02:54