Spring MVC 搭建
1.新建一个 Java Web 项目
1-1 File > New >other



1.2 再 点击 Next 之后把 两个都勾选上 如下图

2 点击项目 > 鼠标右键 > MyEclise > Project Facets > Install Spring Facet

2.1 直接点击 Finish 完成之后 效果 如下图

搭建 Spring 框架最重要的步骤应该就是配置了。官网对框架的解释说明如下:
Spring MVC 框架是围绕一个 DispatcherServlet 来设计的,这个 Servlet 会把请求分发给各个处理器,并支持可配置的处理器映射、视图渲染、本地化、时区与主题渲染等,甚至还能支持文件上传。处理器是你的应用中注解了 @Controller 和 @RequestMapping 的类和方法,Spring 为处理器方法提供了极其多样灵活的配置。
所以,首先我们应该在/WebContent/WEB-INF/web.xml文件,接下来在这个文件中配置 DispatcherServlet。
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

因为这里是把 DispatcherServlet 命名为 springMVC,并且让它在 Web 项目一启动就加载。
接下来我们需要在/WebContent/WEB-INF/目录下创建一个 springMVC-servlet.xml的Spring配置文件。
创建好之后 插入一下代码 :
<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:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-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">
<!-- 使用默认的注解映射 -->
<mvc:annotation-driven />
<mvc:resources location="/" mapping="/index.html" />
<!-- 自动扫描controller包中的控制器 -->
<context:component-scan base-package="cn.mayongfa.api.controller" />
<context:component-scan base-package="cn.mayongfa.controller" />
<!-- 上传文件拦截,设置最大上传文件大小 30M=30*1024*1024(B)=31457280 bytes -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="31457280" />
</bean>
</beans>
接下来就是配置 项目下的/src/applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 将多个配置文件读取到容器中,交给Spring管理 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations"> <list> <value>classpath:global.properties</value>
<value>classpath:jdbc.properties</value> </list> </property>
</bean>
<!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 -->
<context:component-scan base-package="cn.mayongfa.common" />
<context:component-scan base-package="cn.mayongfa.service" />
<context:component-scan base-package="cn.mayongfa.dao" />
<!--master 配置数据源 -->
<bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName">
<value>${master.jdbc.driverClassName}</value>
</property> <property name="url">
<value>${master.jdbc.url}</value>
</property>
<property name="username">
<value>${master.jdbc.username}</value>
</property>
<property name="password">
<value>${master.jdbc.password}</value>
</property>
... </bean>
<!--slave 配置数据源 -->
<bean id="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> ... </bean>
<bean id="dataSource" class="cn.mayongfa.service.imp.DynamicDataSource">
<property name="targetDataSources">
<map> <entry key="slave" value-ref="slaveDataSource" /> </map>
</property>
<property name="defaultTargetDataSource" ref="masterDataSource" />
</bean>
<!-- 配置Jdbc模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource">
</property>
</bean>
<!-- 配置事务管理器 -->
...
<!-- 通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 -->
...
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
上面只是简单的配置,文件并不完整,到这里,其实我们已经配置完成了,接下来就是新建我们需要的 Package 包,来实现不同包来完成不同的事儿的。
按照正常的分层架构一般都会分为 View 层(视图层)、Action 层、Service 层(业务层)、Dao 层(持久层),这里我们也是这样做的,下面就开始新建包

到这里,搭建 Spring MVC 框架的工作算是完成了。
最后 附上 spring 框架 所需 jar 包 下载地址 :http://projects.spring.io/spring-framework/
祝你好运 (∩_∩) ~~~~~~~~~~
Spring MVC 搭建的更多相关文章
- Spring mvc 搭建Mybatis
本文建立在spring mvc已经搭建起来的基础上. 首先看要引入的jar包,其中高亮的是为了mybatis新引入的. <properties> <spring.webm ...
- Spring MVC——搭建HelloWeb工程
1.确保环境配置配置正确(Myeclipse(eclipse)+Tomcat) 2.新建web project 3.将Spring MVC所需的jar包粘贴到WebRoot/WEB-INF/lib下 ...
- Spring MVC 搭建过程中web.xml配置引入文件的路径问题
为啥要说一下这么low的问题,因为我是一个比较low的人,哈哈.本来我技术有限,没事干自己撘个环境找找乐趣,结果被各种基础问题,弄的一脸蒙蔽.算了不多说,直接说问题. 1.首先说一下java编译后的文 ...
- Spring MVC 搭建web项目示例
环境为Eclipse 1:新建Dynamic web project : springMvcDemo 2:下载spring的jar包,把jar包复制到WEB-INF/lib目录下 3.添加配置文件w ...
- 利用Spring MVC搭建REST Service
之前写过一篇 利用JAX-RS快速开发RESTful 服务 今天来看下spring-mvc框架如何实现类似的功能: 一.pom.xml <?xml version="1.0" ...
- Spring MVC前台使用html页面作为视图,配置静态资源后Controller控制器不起作用的解决办法
1.Spring MVC搭建项目的时候,想使用html页面作为前端的视图,你会发现html页面不能访问,原因是由于Spring拦截器将其拦截寻找控制器的缘故,解决办法就是配置静态资源: <mvc ...
- 我是如何进行Spring MVC文档翻译项目的环境搭建、项目管理及自动化构建工作的
感兴趣的同学可以关注这个翻译项目 . 我的博客原文 和 我的Github 前段时间翻译的Spring MVC官方文档完成了第一稿,相关的文章和仓库可以点击以下链接.这篇文章,主要是总结一下这个翻译项目 ...
- MAC OS X El CAPITAN 搭建SPRING MVC (1)- 目录、包名、创建web.xml
一. 下载STS(Spring Tool Suite) 官方地址:http://spring.io/tools/sts 下载spring tool suite for mac 最新版本.这个IDE是很 ...
- Spring MVC篇一、搭建Spring MVC框架
本项目旨在搭建一个简单的Spring MVC框架,了解Spring MVC的基础配置等内容. 一.项目结构 本项目使用idea intellij创建,配合maven管理.整体的目录结构如图: 其中ja ...
随机推荐
- CORS:source,princple,implimentation in Spring
CORS = Cross-Origin Resource Sharing 一种跨域访问技术 什么是 Origin 要理解CORS,首先得理解什么是 “Origin”.参见RFC645 ...
- 使用Java SDK实现离线签名
严格来说,tx-signer并不属于SDK,它是bytomd中构建交易.对交易签名两大模块的java实现版.因此,若想用tx-signer对交易进行离线签名,需要由你在本地保管好自己的私钥. 如果你的 ...
- Linux常用命令——网络命令
Linux常用命令——网络命令 Linux ifconfig 描述:查看设置网络IP 安装命令:yum -y install net-tools 语法:ifconfig 示例:ifconfig et ...
- wireshark基础学习—第一部分wireshark的基础知识
1.Wireshark主窗口 Wireshark的主窗口如下所示 2.每个面板的内容 Packet List(数据包列表): 最上面的面板用表格显示了当前不惑文件中的所有数据包,其中包括了数据包序号. ...
- 在linux上运行网页程序及日志文件查看
- Dart学习-操作符
dart定义了下表所示的运算符.你可以重写许多这些运算符. 描述 运算符 一元后缀 expr++ expr-- () [] . ?. 一元前缀 -expr !expr ~expr ++expr --e ...
- c#格林治时间实现
C#时间戳的简单实现 Introduction: 在项目开发中,我们都经常会用到时间戳来进行时间的存储和传递,最常用的Unix时间戳(TimeStamp)是指格林尼治时间1970年1月1日0时(北 ...
- Android屏幕适配框架-(今日头条终极适配方案)
在Android开发中,屏幕适配是一个非常头痛的问题,因而为了去进行屏幕适配,作为程序员,是呕心沥血,历经磨难,哈哈 我们之前做屏幕适配一般都会用到一下两种方式: 第一种就是宽高限定符适配,什么是宽高 ...
- 工作中遇到的问题——mysql关于年龄,性别的统计
终于暂时闲下来了,一个项目加班加点一年多,前面太忙就顾不上博客了,慢慢的就懈怠了,最近算是暂时闲下来了,项目已经验收进入后期维护阶段,每天空余的时间也多了,想重新拾起博客,不求写什么高深的东西,以后就 ...
- Linux重要命令总结
tar命令后面最常跟的参数 -zxvf 是什么意思? z 表示 tar 包是被 gzip 压缩过的,所以解压时需要用 gunzip 解压 x 从 tar 包中把文件提取出来,单词extract的含义是 ...