0

I am using spring framework for programming, actually I created a simple web app, but some time i will get answer and some time get error

spring-mvc-demo-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"
    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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Step 3: Add support for component scanning -->
    <context:component-scan base-package="com.luv2code.springdemo/home" />

    <!-- Step 4: Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Step 5: Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>


</beans>

web.xml

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

<display-name>Test</display-name>

<!-- Spring MVC Configs -->

<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-mvc-demo-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

HomeController.java

package com.luv2code.springdemo.mvc;

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

@Controller
public class HomeController {

    @RequestMapping("/home")
    public String showPage() {
        return "main-menu";
    }
}

main-menu.jsp

<!DOCTYPE>
<html>

<body>

<h2>Spring MVC Demo - Home Page</h2>

</body>

</html>
Jowhar
  • 1
  • 1
  • 1
  • 1
    Possible duplicate of [No mapping found for HTTP request with URI.... in DispatcherServlet with name](https://stackoverflow.com/questions/18683847/no-mapping-found-for-http-request-with-uri-in-dispatcherservlet-with-name) – Adam Ostrožlík Oct 15 '18 at 09:14
  • The same thread i didn't get the answer. – Jowhar Oct 15 '18 at 09:32
  • will you post the exception root trace or the log of your application ? – Amit Kumar Lal Oct 15 '18 at 10:22
  • I tried different project name, but the answer always. WARNING [http-nio-8084-exec-86] org.springframework.web.servlet.DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI [/spring-mvc-demo/] in DispatcherServlet with name 'dispatcher' – Jowhar Oct 15 '18 at 13:43

2 Answers2

0

I have tried the same code all you have to do is

<context:component-scan base-package="com.sample.springdemo" />

i.e the package name

http://localhost:8081/Test/home will work perfectly fine.

also please look into below link if that is what you need HTTP Status 404 - on Eclipse with Tomcat

Amit Kumar Lal
  • 5,537
  • 3
  • 19
  • 37
  • Not yet. first I tried to Eclipse, now I tried to net beans, but i didn't get the answer – Jowhar Oct 15 '18 at 13:37
  • The main purpose of the above question is I need to know why some time working and sometime not – Jowhar Oct 15 '18 at 13:47
0

I had the same error and the solution is to simply look at the component scan tag in the spring-mvc-demo-servlet.xml file. You're using the instructor's com.luv2code yet your package name is different.

shivlal kumavat
  • 868
  • 1
  • 12
  • 28