一个简单的SpringMVC需要哪些jar包 Spring4

                       

有一次在做项目的时候,我把spring-framework-4.2.3.RELEASE下libs文件下的所有jar包都丢进去了,后来浩哥看了说这怎么行?

今天整理一下一个用springMVC写得helloworld需要依赖哪些包

我们配置一个springMVC的时候

首先是配置web.xml

将请求交给spring的DispatcherServlet处理
代码如下

<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>springmvctestdisplay-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>springmvcservlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>                <load-on-startup>1load-on-startup>    servlet>    <servlet-mapping>        <servlet-name>springmvcservlet-name>        <url-pattern>/url-pattern>    servlet-mapping>web-app>
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

其次配置springmvc-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="com.test.controller" >        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>    context:component-scan>        <mvc:view-controller path="/" view-name="index"/>        <mvc:annotation-driven />        <mvc:resources location="/assets/" mapping="/assets/**">mvc:resources>        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/pages/"/>        <property name="suffix" value=".jsp"/>    bean>beans>
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

根据DispatcherServlet的完整路径来看,我们需要加入

spring-web-4.x.x.RELEASE.jar
    
    
    
    
  • 1

接着启动服务查看报错并加入相关的jar

错误如下:

java.lang.NoClassDefFoundError: org/springframework/beans/factory/BeanNameAware
    
    
    
    
  • 1

根据错误加入

spring-beans-4.x.x.RELEASE.jar
    
    
    
    
  • 1

接着报错

java.lang.NoClassDefFoundError: org/springframework/context/EnvironmentAware
    
    
    
    
  • 1

继续加入

spring-context-4.x.x.RELEASE.jar
    
    
    
    
  • 1

接着报错

java.lang.NoClassDefFoundError: org/springframework/core/env/Environment
    
    
    
    
  • 1

继续加人

spring-core-4.x.x.RELEASE.jar
    
    
    
    
  • 1

接着报错

org/apache/commons/logging/LogFactory
    
    
    
    
  • 1

继续加入

commons-logging-1.1.3.jar
    
    
    
    
  • 1

接着报错

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    
    
    
    
  • 1

嗯?这个错是说找不到DispatcherServlet吗,可是在刚开始都已经将

spring-web-4.x.x.RELEASE.jar
    
    
    
    
  • 1

加入了吗?
原来DispatcherServlet虽然前缀是org.springframework.web.servlet可是它并不在spring-web-4.x.x.RELEASE.jar中
它在

spring-webmvc-4.x.x.RELEASE.jar
    
    
    
    
  • 1

中,好,将其加入
接着报错

java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
    
    
    
    
  • 1

引入

spring-aop-4.x.x.RELEASE.jar
    
    
    
    
  • 1

接着报错

java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
    
    
    
    
  • 1

引入

spring-expression-4.x.x.RELEASE.jar
    
    
    
    
  • 1

到这里已经不报错了

然后开发Controller

写一个helloworld

package com.test.controller;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controller@RequestMapping(value="/hello")public class HelloController {    @RequestMapping(value="/world",method=RequestMethod.GET)    public String hello(Model model){        model.addAttribute("msg", "你好spring mvc");        return "index";    }}
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

最后开发展示层

<%@ 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>hellotitle>head><body><h1>Hello Springh1>${msg }body>html>
    
    
    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

好了打开浏览器访问
http://localhost:8080/springmvctest/hello/world.html

这里写图片描述

一个简单的helloworld完成

最后的最后贴上项目结构和jar包的结构还有下载jar包的地址

我的Dynamic Web Module 是3.0的

项目结构

这里写图片描述

jar包下载地址

推荐使用Maven中央仓库,当然这个项目也可以建成一个maven项目

Maven中央仓库地址

http://mvnrepository.com/

           

你可能感兴趣的:(一个简单的SpringMVC需要哪些jar包 Spring4)