0

I mapped the controller to "/home" that returns "home" which is in WEB-INF folder and I made the prefix to "/WEB-INF/" but I get the same error repeatedly and I using spring with maven

home.jsp is inside the /WEB-INF/ and the controller return home when we made "/home" request and the prefix and suffix are correctly mapped

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>security-spring</groupId>
    <artifactId>007-spring-security</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>007-spring-security Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>

        <!-- Spring MVC support -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>

        <!-- Servlet, JSP and JSTL support -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

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

    </dependencies>
    <build>
        <finalName>007-spring-security</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

AppConfig.java

package com.demo.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.demo.security")
public class AppConfig {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

        viewResolver.setPrefix("/WEB-INF/");
        viewResolver.setSuffix(".jsp");

        return viewResolver;
    }

}

DispatcherServletInitializer.java

package com.demo.security.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] {AppConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/*" };
    }

}

HomeController.java

package com.demo.security.controller;

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

@Controller
public class HomeController {

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

}
ilkerkaran
  • 4,214
  • 3
  • 27
  • 42

1 Answers1

0

Whenever I was trying with your code I was getting org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request on my tomcat console and getting 404 on the browser. By changing URL pattern from /* to / solved the problem.

@Override
protected String[] getServletMappings() {
      return new String[] { "/" };
}
gprasadr8
  • 789
  • 1
  • 8
  • 12
  • i tried it already but that actually not working.... "/home" request will give me 404 error always.. – Suresh Kumar Vairamuthu May 08 '19 at 07:23
  • @SureshKumarVairamuthu may I know what is the error in the server console – gprasadr8 May 08 '19 at 09:14
  • @SureshKumarVairamuthu I found one similar [stack overflow question](https://stackoverflow.com/questions/41577234/why-does-spring-mvc-respond-with-a-404-and-report-no-mapping-found-for-http-req) hope will help you. – gprasadr8 May 08 '19 at 09:20
  • @SureshKumarVairamuthu do you have home.jsp inside WEB-INF folder with exact name match. because I tried with your configuration exactly and it's working fine after change it from /* to / – gprasadr8 May 08 '19 at 11:17