0

I know that maybe other ppl already asked this question and I checked all of the relevant questions here at stackoverflow but I can't fix my problem and I was hoping someone from you can help me. I'm learning Spring MVC right now and I deal with simple pages for now. For some reason my code can't load even though I to everything correct(Or at least I think that I do). When I try to access the localhost:8080/PROJECTNAME/welcome I get Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

The Error from Java is: No mapping found for HTTP request with URI [/ProjetName/welcome] in DispatcherServlet with name 'spring-dispatcher'.

I Use Apache Tomcat 8.5, which is integrated in the Eclipse IDE And also JDK 8

Here is my Code.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>MySpringMVCWebApp</display-name>

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

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

 </web-app>

spring-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"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
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="org.mypackagename.com.*"> 
</context:component-scan>
 <bean id="viewResolver" 
        class = 
  "org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name = "prefix" value ="/WEB-INF/jsp/"/>
  <property name = "suffix" value =".jsp"/>
  </bean>

HelloController.java

package org.mypackagename.com;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

@RequestMapping("/welcome")
public ModelAndView helloWorld() {
    ModelAndView mav = new ModelAndView("HelloPage");
    mav.addObject("msg", "Helloooooooooooo");
    return mav;

  }


 }

HelloPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Spring MVC</title>
</head>
<body>
<h2>${msg}</h2>

 </body>
 </html>

HelloController.java was changed in order to work below is the updated code:

@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)public String printHello(ModelMap 
model) {
  model.addAttribute("message", "Hello the fcking Spring MVC Framework!");
  return "hello";
 }
3
  • <mvc:annotation-driven /> add this to spring config Commented Aug 24, 2018 at 19:04
  • @Arun I've already tried. It doesn't help. Besides xmlns:context = "http://www.springframework.org/schema/context" which is also added does the same work and when I try with <mvc:annotation-driven /> I get the error that this thing is not bound Commented Aug 24, 2018 at 19:07
  • @RequestMapping(value = "/welcome", method = GET) add this to your controller Commented Aug 25, 2018 at 7:12

2 Answers 2

1

Change your servlet-mapping as below

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

specifying url-pattern as / only matches until host/servlet or 'localhost:8080/PROJECTNAME/'

Update:

Configure default or home page in your spring controller as below.

Instead of

@RequestMapping("/welcome")

do

@RequestMapping(value = "/", method = GET)
Sign up to request clarification or add additional context in comments.

4 Comments

There is no error withing the Eclipse IDE i.e. in the console of the server but rather when I try to load up the page. The error is: Type Status Report Message /SpringMVCProject/welcome Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. Apache Tomcat/8.5.33
Check out my code for the HelloController.java but then I just put in the browser tab http://localhost:8080/SpringMVCProject/welcome
I saw your updated answer. However just replacing @RequestMapping("/welcome") by @RequestMapping(value = "/", method = GET) doesn't not help, because it doesn't know what GET is and it wants me to create new constant as it cannot be resolved to variable which doesn't help. Maybe I should change the whole method which comes after it ? I mean this part public ModelAndView helloWorld() {
Your answer wasn't quite right, but it helped me find the correct one. So I think I'm gonna give you the right answer point as it was the thing that gave me the idea to change my method.
0

<url-pattern>/</url-pattern> You didn't specify any application context. So either add PROJECTNAME to your mapping or access it through localhost:8080/welcome

3 Comments

I tried to add <servlet-mapping> <servlet-name>spring-dispatcher</servlet-name> <url-pattern>/SpringMVCProject</url-pattern> </servlet-mapping> and also /SpringMVCProject/welcome but still an error HTTP 404 Not Found
I didn't add it. When I was writing the code and you close the > Eclipse adds it automatically. Anyway, I just removed it and added only <context:component-scan base-package="org.mypackagename.com.*"/> but still the same problem
Sorry, it's my mistake)

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.