Richard
Richard

Reputation: 165

Java Spring MVC mapping issue

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";
 }

Upvotes: 0

Views: 588

Answers (2)

Sundararaj Govindasamy
Sundararaj Govindasamy

Reputation: 8495

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)

Upvotes: 1

Eduard Khachirov
Eduard Khachirov

Reputation: 118

<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

Upvotes: 0

Related Questions