简体   繁体   中英

Spring MVC DispatcherServlet error

I was trying to execute Spring MVC addition example using with Maven but when I try to run the index.jsp page I'm getting this error:

HTTP Status 500 - Error instantiating servlet class org.springframework.web.servlet.DispatcherServlet java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet while executing spring mvc

    pom.xml (to include the jars)   
--------
    <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>com.spring1</groupId>
      <artifactId>mvcexample</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>jar</packaging>

      <name>mvcexample</name>
      <url>http://maven.apache.org</url>

      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>

      <dependencies>
        <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.1.8.RELEASE</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.1.8.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.36</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
      </dependencies>
    </project>

1.pom to add maven dependencies

    index.jsp
    ---------
    <html>
    <body>
    <form action="add">
    <input type="text" name="t1"><br>
    <input type="text" name="t2"><br>
    <input type="text" name="t3"><br>
    <input type="submit" value="submit">
    </form>
    </body>
    </html>

2.the page which is used to take input

    display.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>
    Result is :<%=request.getAttribute("result") %>
    </body>
    </html>

3.The display page to display the result

    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>
      <display-name>Archetype Created Web Application</display-name>
      <servlet>
            <servlet-name>spring1</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>spring1</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

    </web-app>

4.To define the dispatcher servlet

    spring1-servlet.xml
    -------------------
    <?xml version="1.0" encoding="UTF-8"?>
    <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"
        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></ctx:annotation-config>
        <ctx:component-scan base-package="com.spring1.mvcexample"></ctx:component-scan>
    </beans>

    App.java
    ---------
    package com.spring1.mvcexample;



    @Controller
    public class App 
    {
        @RequestMapping("/add")
        public ModelAndView add(HttpServletRequest request,HttpServletResponse response)
        {
            int i=Integer.parseInt(request.getParameter("t1"));
            int j=Integer.parseInt(request.getParameter("t2"));
            int k=Integer.parseInt(request.getParameter("t3"));
            int l=i+j+k;
            ModelAndView mv=new ModelAndView();
            mv.setViewName("display.jsp");
            mv.addObject("result",l);
            return mv;
        }
        }

5.The code which contains controller

I have mentioned all classes and files I have used. I tried many times but could not resolve the error. Please suggest me how I can resolve the error. Thanks in advance.

You can simply add following tag load-on-startup under web.xml

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>spring1</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring1</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app> 

you are missing some dependency jars in your pom.xml , kindly add those, some dependecy that i can see which are missing are

  1. org.springframework.beans-3.0.1.RELEASE-A

  2. org.springframework.core-3.0.1.RELEASE-A

  3. org.springframework.web-3.0.1.RELEASE-A

  4. org.springframework.web.servlet-3.0.1.RELEASE-A

  5. org.springframework.web.portlet-3.0.1.RELEASE-A

  6. org.springframework.web.struts-3.0.1.RELEASE-A

write below line under <servlet> tag:

  • <load-on-startup>1</load-on-startup>

Also make sure your web.xml and spring1-servlet.xml files should be under /WEB-INF folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM