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 搭建的更多相关文章

  1. Spring mvc 搭建Mybatis

    本文建立在spring mvc已经搭建起来的基础上. 首先看要引入的jar包,其中高亮的是为了mybatis新引入的. <properties>       <spring.webm ...

  2. Spring MVC——搭建HelloWeb工程

    1.确保环境配置配置正确(Myeclipse(eclipse)+Tomcat) 2.新建web project 3.将Spring MVC所需的jar包粘贴到WebRoot/WEB-INF/lib下 ...

  3. Spring MVC 搭建过程中web.xml配置引入文件的路径问题

    为啥要说一下这么low的问题,因为我是一个比较low的人,哈哈.本来我技术有限,没事干自己撘个环境找找乐趣,结果被各种基础问题,弄的一脸蒙蔽.算了不多说,直接说问题. 1.首先说一下java编译后的文 ...

  4. Spring MVC 搭建web项目示例

    环境为Eclipse 1:新建Dynamic web project  : springMvcDemo 2:下载spring的jar包,把jar包复制到WEB-INF/lib目录下 3.添加配置文件w ...

  5. 利用Spring MVC搭建REST Service

    之前写过一篇 利用JAX-RS快速开发RESTful 服务 今天来看下spring-mvc框架如何实现类似的功能: 一.pom.xml <?xml version="1.0" ...

  6. Spring MVC前台使用html页面作为视图,配置静态资源后Controller控制器不起作用的解决办法

    1.Spring MVC搭建项目的时候,想使用html页面作为前端的视图,你会发现html页面不能访问,原因是由于Spring拦截器将其拦截寻找控制器的缘故,解决办法就是配置静态资源: <mvc ...

  7. 我是如何进行Spring MVC文档翻译项目的环境搭建、项目管理及自动化构建工作的

    感兴趣的同学可以关注这个翻译项目 . 我的博客原文 和 我的Github 前段时间翻译的Spring MVC官方文档完成了第一稿,相关的文章和仓库可以点击以下链接.这篇文章,主要是总结一下这个翻译项目 ...

  8. 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是很 ...

  9. Spring MVC篇一、搭建Spring MVC框架

    本项目旨在搭建一个简单的Spring MVC框架,了解Spring MVC的基础配置等内容. 一.项目结构 本项目使用idea intellij创建,配合maven管理.整体的目录结构如图: 其中ja ...

随机推荐

  1. Python day 05

    day05 数据类型 今日内容 字典 补充 .extend() users = [1,2,3,4] people = [5,6,7,8] users.extend(people) people.ext ...

  2. shell编程(七)之字符串处理

    字符串切片 ${var:offset:number} 取字符串的最右侧几个字符: ${var: -length} 注意: 冒号后必须有一个空白字符 #!/bin/bash var="chen ...

  3. blogger添加代码高亮

    blogger(blogspot)自带的是没有代码高亮的,我们可以用下面的方法添加代码高亮. 首先我们打开blogger(blogspot)后台,然后点击主题背景-->修改html,然后在弹出的 ...

  4. Java中封装类型.valueOf()

    @Test public void test() { Integer i3 =128; Integer i4=128; boolean integerGT127=i3==i4; //false Sys ...

  5. Python开发 基础篇

    2019-02-01 产生验证码: 用户输入的值和显示的值相同时显示Correct,否则继续生成随机验证码等待用户输入 def check_code(): import random checkcod ...

  6. vue项目知识点总结

    一.vue中如何获取select被选中的id和对应的值. <!-- 下拉框 --> <div v-show="moreStore" class="sel ...

  7. [Dababase - MySQL- Linux] 数据库安装位置

    数据库安装最好是安装在 usr/local/ 里面,因为默认的很多东西都是指向这个文件夹中的.

  8. linux debug tools

    linux modules: IO schedule.VFS.OOM.memory.net.process schedule kernel debug: perf(IO bound\CPU bound ...

  9. HBase表数据的转移之使用自定义MapReduce

    目标:将fruit表中的一部分数据,通过MR迁入到fruit_mr表中 Step1.构建ReadFruitMapper类,用于读取fruit表中的数据 package com.z.hbase_mr; ...

  10. mysql5.7 timestamp错误:there can be only oneTIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE

    #1293 - Incorrect table definition; there can be only oneTIMESTAMP column with CURRENT_TIMESTAMP in ...