2

I am a beginner to Spring i am learning from tutorials point , Below code is exactly as it is in the Tutorials point but when i try to run it i am getting 404 error, the code is hello world program when i run on eclipse using tomcat server i am getting error like this

The url i am using on browser is http://localhost:8080/HelloWeb/hello

HelloController.java:

package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;

@Controller
@RequestMapping("/hello")
public class HelloController{

   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");

      return "hello";
   }

}

HelloWeb-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.tutorialspoint" />

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

</beans>

web.xml:

<web-app id="WebApp_ID" version="2.4"
   xmlns="http://java.sun.com/xml/ns/j2ee" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <display-name>Spring MVC Application</display-name>

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

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

</web-app>

hello.jsp:

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h2>${message}</h2>
</body>
</html>

and this is my directory structure

HelloWeb Project:

|--HelloController.java
|-- WebContent
   `-- WEB-INF
         |-- jsp
              |-- web.xml
12
  • What URL did you try to access? Can you see the Tomcat homepage when you access http://localhost:8080/ ? Commented Aug 14, 2015 at 6:39
  • what url are you hitting? Commented Aug 14, 2015 at 6:40
  • Yeah i am able to access tomcat home page localhost:8080 Commented Aug 14, 2015 at 6:40
  • 1
    try hitting localhost:8080/HelloWeb/hello Commented Aug 14, 2015 at 6:41
  • 1
    The whole point of an MVC framework is to NEVER hit a view (a JSP) directly, but to ALWAYS go through a controller. And BTW, everything under WEB-INF is, by design, inaccessible from the outside. Hit the URL of the controller: /HelloWeb/hello. Also, the web.xml file must be directly under WEB-INF. Not under WEB-INF/jsp Commented Aug 14, 2015 at 6:43

1 Answer 1

1

http://howtodoinjava.com/2015/02/05/spring-mvc-hello-world-example/#hello_world

Follow the above URL , it works completely I have tried and make sure you have added dependencies for spring in pom.xml. Also in your webapp folder HelloWeb-servlet.xml should also be present.

Sign up to request clarification or add additional context in comments.

Comments

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.