有一次在做项目的时候,我把spring-framework-4.2.3.RELEASE下libs文件下的所有jar包都丢进去了,后来浩哥看了说这怎么行?
今天整理一下一个用springMVC写得helloworld需要依赖哪些包
我们配置一个springMVC的时候
首先是配置web.xml
将请求交给spring的DispatcherServlet处理
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 id="WebApp_ID" version="3.0"> 6 <display-name>springmvctest</display-name> 7 <!-- 统一编码 --> 8 <filter> 9 <filter-name>charsetEncoding</filter-name> 10 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 11 <init-param> 12 <param-name>encoding</param-name> 13 <param-value>UTF-8</param-value> 14 </init-param> 15 <init-param> 16 <param-name>forceEncoding</param-name> 17 <param-value>true</param-value> 18 </init-param> 19 </filter> 20 <filter-mapping> 21 <filter-name>charsetEncoding</filter-name> 22 <url-pattern>/*</url-pattern> 23 </filter-mapping> 24 <!-- 前端控制器 --> 25 <servlet> 26 <servlet-name>springmvc</servlet-name> 27 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 28 <!-- 加载/WEB-INF/[servlet-name]-servlet.xml --> 29 <load-on-startup>1</load-on-startup> 30 </servlet> 31 <servlet-mapping> 32 <servlet-name>springmvc</servlet-name> 33 <url-pattern>/</url-pattern> 34 </servlet-mapping> 35 </web-app>
其次配置springmvc-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xmlns:task="http://www.springframework.org/schema/task" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans 10 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-4.2.xsd 13 http://www.springframework.org/schema/mvc 14 http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 15 http://www.springframework.org/schema/task 16 http://www.springframework.org/schema/task/spring-task-4.2.xsd"> 17 18 <!-- 扫描路径 --> 19 <context:component-scan base-package="com.test.controller" > 20 <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 21 </context:component-scan> 22 23 <!-- 配置根视图 --> 24 <mvc:view-controller path="/" view-name="index"/> 25 26 <!-- 激活基于注解的配置 @RequestMapping, @ExceptionHandler,数据绑定 ,@NumberFormat , 27 @DateTimeFormat ,@Controller ,@Valid ,@RequestBody ,@ResponseBody等 --> 28 <mvc:annotation-driven /> 29 30 <!-- 静态资源配置 --> 31 <mvc:resources location="/assets/" mapping="/assets/**"></mvc:resources> 32 33 <!-- 视图层配置 --> 34 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 35 <property name="prefix" value="/WEB-INF/pages/"/> 36 <property name="suffix" value=".jsp"/> 37 </bean> 38 39 </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
1 package com.test.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.Model; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 8 @Controller 9 @RequestMapping(value="/hello") 10 public class HelloController { 11 @RequestMapping(value="/world",method=RequestMethod.GET) 12 public String hello(Model model){ 13 model.addAttribute("msg", "你好spring mvc"); 14 return "index"; 15 } 16 }
最后开发展示层
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>hello</title> 8 </head> 9 <body> 10 <h1>Hello Spring</h1> 11 ${msg } 12 </body> 13 </html>
好了打开浏览器访问
http://localhost:8080/springmvctest/hello/world.html
一个简单的helloworld完成
最后的最后贴上项目结构和jar包的结构还有下载jar包的地址
我的Dynamic Web Module 是3.0的
项目结构
推荐使用Maven中央仓库,当然这个项目也可以建成一个maven项目
Maven中央仓库地址