1

I am trying to make a spring hello world program in eclipse. Here is the code

index.jsp

<a href="hello.html">click</a>

HelloWorldController.java

package com.javatpoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorldController {
    @RequestMapping("/hello")
    public ModelAndView helloWorld() {
        String message = "HELLO SPRING MVC HOW R U";
        return new ModelAndView("hellopage", "message", message);
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

spring-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-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.javatpoint" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

hellopage.jsp

Message is: ${message}

When I go to http://localhost:8080/SpringMVCBasic/, I gets the index.jsp page correctly open

index.jsp OutPut

but when I click on link on the page i.e. the url http://localhost:8080/SpringMVCBasic/hello.html, I gets the error

HTTP Status 404 -

When I use url like this http://localhost:8080/SpringMVCBasic/hello, I gets this

HTTP Status 404 - /SpringMVCBasic/hello

I have taken the code from here

3
  • 1
    You should change the link from "hello.html" to "hello" like it is defined in the controller. Commented Jan 19, 2016 at 7:25
  • try by changing the URL pattern as <url-pattern>/*.html</url-pattern> Commented Jan 19, 2016 at 7:40
  • What is your context-path? Are you using Eclipse or Intellij Idea. Anything you see in console. Commented Jan 19, 2016 at 10:31

5 Answers 5

2

You provided mapping for:

@RequestMapping("/hello")

but you have to do it for:

@RequestMapping("/hello.html")
Sign up to request clarification or add additional context in comments.

3 Comments

Did you put hellopage.jsp into WEB-INF/jsp/ directory?
Yes, Above I mentioned the site from where I downloaded the whole project & tried to ran as it is.
have you added Spring Core jar files and Spring Web jar files
0

I downloaded the eclipse project and run it on a jboss. Following jars needed to be included:

  • spring-context
  • spring-core
  • spring-web
  • spring-webmvc
  • spring-aop
  • spring-beans
  • spring-expression

If i afterwards run the jboss with Java 8 all seems to work fine.

Comments

0

Enables the Spring MVC @Controller programming model in spring-servlet.xml like this:

<mvc:annotation-driven />

Comments

0

In your index.jsp instead of writing

href="hello.html">
change to
href="./hello.html">

and

In HelloworldController.java instead of writing

@RequestMapping("/hello")

change to:-

 @RequestMapping(value="/hello.html")

Comments

-1

Please add all these jar as external jar or add these jar to your lib folder

Spring Jar

1 Comment

I already added all the spring framework 4.1.1 JAR files via the properties of eclipse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.