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 ...
随机推荐
- 【HNOI 2018】转盘
Problem Description 一次小 \(G\) 和小 \(H\) 原本准备去聚餐,但由于太麻烦了于是题面简化如下: 一个转盘上有摆成一圈的 \(n\) 个物品(编号 \(1\) 至 \(n ...
- 【NET Core】Nuget包发布流程
1.新建一个.NET Core类库 2.新增一个方法,并编译项目 3.下载Nuget.exe,与刚才新建的类库放在同一目录下 下载地址:https://www.nuget.org/downloads ...
- LoRa无线技术介绍
什么是LoRa LoRa是semtech公司创建的低功耗局域网无线标准,低功耗一般很难覆盖远距离,远距离一般功耗高,要想马儿不吃草还要跑得远,好像难以办到.LoRa的名字就是远距离无线电(Long R ...
- Go语言学习之11 日志收集系统kafka库实战
本节主要内容: 1. 日志收集系统设计2. 日志客户端开发 1. 项目背景 a. 每个系统都有日志,当系统出现问题时,需要通过日志解决问题 b. 当系统机器比较少时,登陆到服务器上查看即可 ...
- Ctrl+Alt+Down/Up 按键冲突
I mapped Ctrl-Alt-Up/Down to open web-browser and email client but it didn't take effect. Ctrl-Alt-U ...
- LeetCode--035--搜索插入位置(java)
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6], 5 输 ...
- Myeclipse6.5每次打开properties中文注释都会变成乱码
发现无论怎么写properties注释,只要重新打开me就会出现乱码.默认properties是不支持中文的.所以最好用英文写properties文档.也可以写好直接翻译.已经写好的乱码直接拖到Chr ...
- HBuild 连接安卓手机
设备:一部电脑.一部安卓手机.一条数据线 1. 数据线连接电脑和安卓手机: 2. 安卓手机--> 设置 -- > 开发者选项 --> 点进去,找到USB调试并且打开,例: ...
- SWUST OJ(1028)
特定字符序列的判断 #include <iostream> #include <cstdlib> #include <stack> #include <str ...
- ROM、RAM、CPU、CACHE、FLASH
内存在电脑中起着举足轻重的作用.内存一般采用半导体存储单元,包括随机存储器(RAM),只读存储器(ROM),以及高速缓存(CACHE).只不过因为RAM是其中最重要的存储器,所以通常所说的内存即指电脑 ...