我尝试使用Maven执行Spring MVC添加示例,但是当我尝试运行index.jsp页面时,我收到此错误:
HTTP状态500 - 实例化servlet类org.springframework.web.servlet.DispatcherServlet时出错 执行spring mvc时java.lang.ClassNotFoundException:org.springframework.web.servlet.DispatcherServlet
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添加maven依赖项
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.用于输入的页面
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.显示结果的显示页面
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.定义调度程序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.包含控制器的代码
我已经提到了我使用的所有类和文件。我尝试了很多次但无法解决错误。请建议我如何解决错误。提前谢谢。
答案 0 :(得分:1)
您只需在web.xml
下添加以下标记load-on-startup<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>
答案 1 :(得分:-1)
你在pom.xml中遗漏了一些依赖关系jar,请添加一些,我可以看到哪些依赖关系是
org.springframework.beans-3.0.1.RELEASE-A
org.springframework.core-3.0.1.RELEASE-A
org.springframework.web-3.0.1.RELEASE-A
org.springframework.web.servlet-3.0.1.RELEASE-A
org.springframework.web.portlet-3.0.1.RELEASE-A
org.springframework.web.struts-3.0.1.RELEASE-A
答案 2 :(得分:-1)
在<servlet>
标记下写下以下行:
<load-on-startup>1</load-on-startup>
还要确保您的web.xml和spring1-servlet.xml文件位于/ WEB-INF文件夹下。