0

I'm constantly getting this error:

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

WARNING [http-nio-8080-exec-6] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'SpringMVC'

I'm trying to use Java configuration on a very simple code. I have this config class:

 package com.kemal.config;

    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;

    @Configuration
    @ComponentScan(basePackages = {"com.kemal", "com.kemal.controllers", "com.kemal.services"})
    public class JavaConfig {

    }

This is my controller class:

package com.kemal.controllers;

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

@Controller
public class Controller1{

    @RequestMapping("/")
    public String home (){
        return "index";
    }

    @RequestMapping("/add")
    public String add(){
        return "display";
    }
}

Tomcat is deploying Application context to this URL "/"

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

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

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

</web-app>

SpringMVC-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:ctx="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/c"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">

<ctx:annotation-config/>
<!--<context:component-scan base-package="com"/>-->

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

My pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.kemal</groupId>
  <artifactId>SpringMVC</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SpringMVC Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.9.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>


  </dependencies>

  <build>
    <finalName>SpringMVC</finalName>
  </build>
</project>

IntelliJ can connect controller class with jsp files referenced in handler methods, but I think server can't find jsp. What am I missing? I can't find a solution right now.

Project structure and facets: enter image description here enter image description here

Project structure

Kvark900
  • 147
  • 1
  • 4
  • 18

2 Answers2

0

Include webmvc annotation @EnableWebMvc in your configuration class javaconfig. It helps to scan @Controller annotation based classes.

  • Please check base url path(or context path) as your deploying in tomcat. If you have not configured any context path, then your project name will be context path. – Mohammed Abdullah Aug 24 '17 at 18:53
  • Tomcat is opening this url http://localhost:8080/index.jsp, but in the log I can see this warning 24-Aug-2017 21:19:26.183 WARNING [http-nio-8080-exec-6] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'SpringMVC' – Kvark900 Aug 24 '17 at 19:21
  • Add wildcard separator in your servlet mapping. Change your url pattern SpringMVC /* hit the url in browser like this : http://localhost:8080// – Mohammed Abdullah Aug 25 '17 at 15:05
0

Add <mvc:annotation-driven /> in your SpringMVC-servlet.xml

<ctx:annotation-config> declares support for general annotations such as @Required, @Autowired, @PostConstruct, and so on.

<mvc:annotation-driven /> declares explicit support for annotation-driven MVC controllers (i.e. @RequestMapping, @Controller, although support for those is the default behaviour), as well as adding support for declarative validation via @Valid and message body marshalling with @RequestBody/ResponseBody.

Amit K Bist
  • 6,760
  • 1
  • 11
  • 26
  • Thank you for your answer, but you should quote: https://stackoverflow.com/questions/3977973/whats-the-difference-between-mvcannotation-driven-and-contextannotation Btw. it doesn't work. I added `` – Kvark900 Aug 24 '17 at 16:07