首先配置web.xml文件
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>oasystemdisplay-name>
<filter>
<filter-name>charsetEncodingfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>charsetEncodingfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>springservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
web-app>
然后是spring-servlet.xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd">
<context:component-scan base-package="oasystem.controller" >
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
bean>
beans>
根据web.xml文件中org.springframework.web.servlet.DispatcherServlet的配置,首先需要导入spring-web-4.x.x.RELEASE.jar这个包。
运行报错
java.lang.NoClassDefFoundError: org/springframework/beans/factory/BeanNameAware
导入spring-beans-4.2.3.RELEASE.jar
运行报错
java.lang.NoClassDefFoundError: org/springframework/context/EnvironmentAware
导入spring-context-4.2.3.RELEASE.jar
运行,会提示服务器启动失败,
在控制台下会有这段报错
Caused by: java.lang.ClassNotFoundException: org.springframework.core.env.Environment
那就导入spring-core-4.2.3.RELEASE.jar
运行,这次可以启动而且报错也变了
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory
OK接着导入commons-logging-1.1.3.jar
这个包不在官网下的spring-framework-4.2.3.RELEASE-dist.zip里,要重新下
运行报错java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
DispatcherServlet没找到?刚开始不是导入过spring-web-4.x.x.RELEASE.jar吗?
原来DispatcherServlet虽然前缀是org.springframework.web.servlet可是它并不在spring-web-4.x.x.RELEASE.jar中
它在
spring-webmvc-4.x.x.RELEASE.jar
但是spring-web-4.x.x.RELEASE.jar也不能删除,删了服务器又无法启动报错
Caused by: java.lang.ClassNotFoundException: org.springframework.web.context.request.async.CallableProcessingInterceptor
接下来运行报错
Caused by: java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
导入spring-aop-4.2.3.RELEASE.jar
运行报错
Caused by: java.lang.ClassNotFoundException: org.springframework.expression.ParserContext
导入spring-expression-4.2.3.RELEASE.jar
运行通过~~~~
所以,最简单的SpringMVC框架需要8个包。
进入controller层
package oasystem.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//注解注入
@Controller
@RequestMapping(value="/user")
public class UserController {
//路径
@RequestMapping(value="/index")
public String user(){
//返回的jsp页面
return "index";
}
}
然后在WEB-INF下建jsp文件,
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主页title>
head>
<body>
这是主页
body>
html>>
保存发布,然后在浏览器里输入
localhost:8080/oasystem/user/index
(端口号8080或者8088/项目名称/Controller类上面注释的路径/类里面访问主页方法上面的路径)
回车就可以访问了。
另外:导入的包名中间的数字是版本号,可以根据实际情况自行选定。各种包都可以去Spring的官方网站上下载。