一个简单的SpringMVC需要哪些jar包[Spring4.1.6]+hibernate4.2.4
今天整理一下一个用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>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/WEB-INF/classes/applicationContext.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/applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!-- 打开Spring的Annotation支持 -->
<context:annotation-config />
<!-- 注解扫描包 -->
<context:component-scan base-package=com.*">
<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>
为什么在Spring的配置里,最好不要配置xsd文件的版本号
参考:
http://blog.csdn.net/hengyunabc/article/details/22295749
根据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.NoClassDefFoundError: org/springframework/aop/TargetSource
引入
spring-aop-4.x.x.RELEASE.jar
接着报错
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/mvc]
Offending resource: ServletContext resource [/WEB-INF/classes/applicationContext.xml]
这个错误不容易找,经反复检验,就是缺少这个
引入
spring-webmvc-4.1.6.RELEASE.jar
接着报错
java.lang.ClassNotFoundException: org.springframework.expression.ParserContext
接着报错
java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
引入
spring-expression-4.x.x.RELEASE.jar
接着报错
Caused by: java.lang.ClassNotFoundException: ModelAndView
引入
spring-webmvc-portlet-4.1.6.RELEASE.jar
这个也容易漏掉。
到这里已经不报错了
然后开发Controller
写一个helloworld
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class hellowController {
public static void main(String[] args) {
System.out.println(1 + "");
}
@RequestMapping("/helloworld")
public String hello() {
System.out.println("hello");
return "success";
}
}
最后展示层
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>My JSP 'index.jsp' starting page</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>
This is my JSP page666. <br>
<a href="helloworld">Hello</a>
</body>
</html>
跳转页面
<%@ 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>跳转成功</title>
</head>
<body>
<h1>success...</h1>
</body>
</html>
打开浏览器访问
http://localhost:8080/study/
跳转成功
helloworld完成了