一个简单的SpringMVC需要哪些jar包[Spring4.x]

转自:http://blog.csdn.net/frankcheng5143/article/details/50512340

之前做项目搭框架都是把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>ElecFarmerManager</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<!-- 统一编码 -->
	<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>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<!-- 前端控制器 -->
	<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/classes/config/springmvc-servlet.xml</param-value>
		</init-param>
		<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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task-4.2.xsd">
 
    <!-- 扫描路径 -->
    <context:component-scan base-package="com.gd">
    	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
 	<!-- 配置根视图 -->
    <mvc:view-controller path="/" view-name="index"/>
    
    <!-- don't handle the static resource -->
    <mvc:default-servlet-handler />
 
    <!-- 激活基于注解的配置 @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"
            id="internalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/jsp/" />
        <!-- 后缀 -->
        <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.rems.modules.framework;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/framework")
public class MvcController {

    public static void main(String[] args) {
        System.out.println(1+"");
    }
    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }
}

最后开发展示层

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>springmvc 'hello.jsp'</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    hello world! <br>
  </body>
</html>

好了打开浏览器访问

http://127.0.0.1:2007/R******ManagementSystem/framework/hello

一个简单的helloworld完成

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

项目结构


jar包推荐官网下载(spring官网:http://spring.io/)

怎么在官网上找到下载地址见我的另一篇文章:

http://blog.csdn.net/xiaoshuji/article/details/52512953

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值