user3784911
user3784911

Reputation: 89

Error displaying jsp page and excel file 'No mapping found for HTTP request with URI

For this application I am trying to export data to an excel spreadsheet user apache poi dependency in my pom.xml. The jsp page does not display. I tried adding a web.xml and a servlet-config.xml and the page does not display, instead it gives me an 404 error "No mapping found for HTTP request with URI [/ApplicationNameErrorCodes/report] in DispatcherServlet with name 'dispatcher'" I have ran out of ideas, I researched google, I looked on youtube for examples, and I cannot find any resolution that will fix the error so I can display the page properly and create and excel file.

package applicationnameanderrorcodes.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import applicationnameanderrorcodes.model.ApplicationNameAndErrorCodes;
import applicationnameanderrorcodes.view.ExcelGenerator;

@Controller
@RequestMapping(value = "/")
public class ExcelController {
    @RequestMapping(value = "/report", method = RequestMethod.GET)
    public ModelAndView applicationAndErrorCode(HttpServletRequest request, HttpServletResponse response) {
        String typeReport = request.getParameter("type");

        List<ApplicationNameAndErrorCodes> list = new ArrayList<ApplicationNameAndErrorCodes>();
        list.add(new ApplicationNameAndErrorCodes("Snabox",
                "08988A", "Add Network", "Chris Avery"));
        list.add(new ApplicationNameAndErrorCodes("Navigation",
                "270056A", "Enter Safe Mode to Unlock", "Tim Smith"));
        list.add(new ApplicationNameAndErrorCodes("Magic Desktop",
                "6892077", "Reset Desktop", "Marcus James"));
        list.add(new ApplicationNameAndErrorCodes("IFiring Portal",
                "60112011", "Deleted extra Attribute", "Aby Martinez"));

        if(typeReport != null && typeReport.equals("xlsx")) {
            return new ModelAndView(new ExcelGenerator(), "listOfErrorCodes",list);
        }

        return new ModelAndView("excelReport", "listOfErrorCodes", list);
    }
}

servlet-config.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:security="http://www.springframework.org/schema/security"
    xsi:schemaLocation="
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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">

        <mvc:annotation-driven/>
        <mvc:default-servlet-handler/>
        <context:component-scan base-package="applicationnameanderrorcodes.controller"/>
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
  <display-name>ApplicationNameErrorCodes</display-name>
  <servlet>
    <servlet-name>ApplicationNameErrorCodes</servlet-name>
    <servlet-class>
           org.springframework.web.servlet.DispatcherServlet
     </servlet-class>
    <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/servlet-config.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>ApplicationNameErrorCodes</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

excelReport.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%--
  Created by IntelliJ IDEA.
  User: steven
  Date: 12/17/17
  Time: 8:45 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Report</title>
</head>
<body>
    <table border="1">
        <thead>
            <tr>
                <td>Application Name</td>
                <td>Error Codde</td>
                <td>Resolution</td>
                <td>User Name</td>
            </tr>
        </thead>
        <tbody>
            <c:forEach items="${listOfErrorCodes}" var="errorCode">
                <tr>
                    <td>errorCode.applicationName</td>
                    <td>errorCode.errorCode</td>
                    <td>errorCode.resolution</td>
                    <td>errorCode.userName</td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
    <spring:url value="/report/?type=xlsx" var="xlsxUrl" />
    <a href="${xlsxUrl}">Download Excel</a>
</body>
</html>

Here's some files I am not sure I need WebConfig.java and a Webinitialzer.java

package applicationnameanderrorcodes.config;

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

@EnableWebMvc
@Configuration
@ComponentScan({"hcscapplicationerror"})
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
}

WebInitializer.java

package applicationnameanderrorcodes.config;

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

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

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

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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>applicationnameanderrorcodes</groupId>
  <artifactId>ApplicationNameErrorCodes</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.0.RELEASE</version>
    </dependency>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.0.RELEASE</version>
    </dependency>
    <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
    </dependency>
    <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
    </dependency>
    <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
    </dependency>
  </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <warSourceDirectory>src/main/webapp</warSourceDirectory>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Upvotes: 1

Views: 189

Answers (1)

Bolaji Salau
Bolaji Salau

Reputation: 11

(1) In a deployment descriptor (web.xml) of a Spring MVC application, Spring MVC requires configuration of a front controller called Dispatcher Servlet and a mapping pattern attached to it to indicate how request are to be routed to method handlers. You have not mapped your Dispatcher Servlet correctly as it has no / (slash) preceding it. The / forward slash indicate that your Dispatcher Servlet is responsible for intercepting request from the root of your application. This is why the reference you are making in your controller to / is of no effect. You can find out more here. Below is a sample from official documentation

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/app-context.xml</param-value>
</context-param>

<servlet>
    <servlet-name>app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>/app/*</url-pattern>
</servlet-mapping>

(2) Also your Deployment descriptor may not be fully defined except you intend to defined all your configurations in the Dispatcher Servlet. To load other resources such as database connectivity, you will need to define a ContextLoaderListener listener for your Dispatcher Servlet in the conextConfigLocation of the context param element of the web.xml. These are minor misunderstandings I suggest you do some further reading

(3) A third suggestion is that you either use XML or Java annotation configuration and not both. Combining both could lead to error if you are still learning their usage.

(4) This should be the problem: You have passed in your Dispatcher Servlet config (WebConfig) as root config. Please define it correctly as indicated.

package applicationnameanderrorcodes.config;

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

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {
     //This is not correct, WebConfig should not be here
    **return new Class[]{ WebConfig.class };**
}

@Override
protected Class<?>[] getServletConfigClasses() {
    //This is correct
    return new Class[]{ WebConfig.class };
}

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

}

Upvotes: 0

Related Questions