1. 程式人生 > >一個簡單的SpringMVC需要哪些jar包[Spring4]

一個簡單的SpringMVC需要哪些jar包[Spring4]

有一次在做專案的時候,我把spring-framework-4.2.3.RELEASE下libs檔案下的所有jar包都丟進去了,後來浩哥看了說這怎麼行?

今天整理一下一個用springMVC寫得helloworld需要依賴哪些包

我們配置一個springMVC的時候

首先是配置web.xml

將請求交給spring的DispatcherServlet處理
程式碼如下

<?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/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>springmvctest</display-name> <!-- 統一編碼 --> <filter> <filter-name>charsetEncoding</filter-name
>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param>
<param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>charsetEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 載入/WEB-INF/[servlet-name]-servlet.xml --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

其次配置springmvc-servlet.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: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"/>

    <!-- 啟用基於註解的配置 @RequestMapping, @ExceptionHandler,資料繫結 ,@NumberFormat ,
    @DateTimeFormat ,@Controller ,@Valid ,@RequestBody ,@ResponseBody等  -->
    <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>

根據DispatcherServlet的完整路徑來看,我們需要加入

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

接著啟動服務檢視報錯並加入相關的jar

錯誤如下:

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

根據錯誤加入

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

接著報錯

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

繼續加入

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

接著報錯

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

繼續加人

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

接著報錯

org/apache/commons/logging/LogFactory

繼續加入

commons-logging-1.1.3.jar

接著報錯

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

中,好,將其加入
接著報錯

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

引入

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

接著報錯

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

引入

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

到這裡已經不報錯了

然後開發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";
    }
}

最後開發展示層

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>hello</title>
</head>
<body>
<h1>Hello Spring</h1>
${msg }
</body>
</html>

這裡寫圖片描述

一個簡單的helloworld完成

最後的最後貼上專案結構和jar包的結構還有下載jar包的地址

我的Dynamic Web Module 是3.0的

專案結構

這裡寫圖片描述

jar包下載地址

推薦使用Maven中央倉庫,當然這個專案也可以建成一個maven專案

Maven中央倉庫地址