user1915570
user1915570

Reputation: 386

Spring Maven DispatcherServlet noHandlerFound

I am new to java world. I am following one tutorial to learn java. The tutorial using Spring framework and maven build. but when I reached the part to use DispatcherServlet, mine doesn't work as it suppose. It shows this on console:

org.springframework.web.servlet.PageNotFound noHandlerFound 

WARNING: No mapping found for HTTP request with URI [/index.html] in DispatcherServlet with name 'dispatcher'

and browser can't load just simple hello world string. browser says

HTTP ERROR 404
Problem accessing /index.xml. Reason:
Not Found
Powered by Jetty://

This seems common problem becasue there are a lot of discussions about this. I have read several of them and tried some answers but nothing helps. Can anyone check my code and point out what might cause this problem?

web.xml

<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" version="3.0">
<display-name>TestApplication</display-name>
<welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
</welcome-file-list>
 <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>*.html</url-pattern>
    <url-pattern>*.htm</url-pattern>
    <url-pattern>*.json</url-pattern>
    <url-pattern>*.xml</url-pattern>
</servlet-mapping>
</web-app>

dispacher-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:context="http://www.springframework.org/schema/context"
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-4.0.xsd">

<context:component-scan base-package="my.testApplication.controller" />
</beans>

IndexController.java

package my.testApplication.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

@Controller
public class IndexController {

  @RequestMapping("/index")
  public String index(){
    return "WEB-INF/jsp/index.jsp";
  }
}

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>Insert title here</title>
</head>
<body>

hello from spring web mvc
</body>
</html>

Additional comment (solution): it was right reference, I made some mistakes and then it didn't work afterwards even with the right code. when this kind of situation (after mistakes and back to right code - still not working), then need to perform 'Project - clean' before run. (maybe the basics.) So that solved. Thanks.

Upvotes: 0

Views: 1966

Answers (1)

minion
minion

Reputation: 4353

It should be @RequestMapping annotation.

@RequestMapping(value="/index")
  public String index(){
    return "WEB-INF/jsp/index.jsp";
  }

Upvotes: 1

Related Questions