spirng: srping mvc配置(访问路径配置)搭建SpringMVC——最小化配置
搭建SpringMVC——最小化配置
最开始接触网页的时候,是纯的html/css页面,那个时候还是用Dreamweaver来绘制页面。
随着网站开发的深入,开始学习servlet开发,记得最痛苦的就是servlet返回网页的内容是字符串拼接的html页面,整不好就无法显示....
再到后来开学学习SSH,庞大的架构眼花缭乱。Struts繁杂的标签、hibernate搞不清楚的数据表,Spring不知道哪里搞错的bean。
最后随着发展,前端开始占有一席之地,nodejs风生水起,很多业务逻辑开始前置。再也看不到当初的bo、dao了,取而代之的是各种框架的mvvm,后台减轻压力只负责一些必要的逻辑。
到现在,好像web开发又发展到了一个阶段——前端由于Nodejs的作用,可以支撑一部分业务逻辑,通过转发代理,统一发往后台。后台通过url实现mvc,对性持久化、更深入的逻辑操作等等。Spring MVC在这里就起了很关键的作用....它通过Url拦截请求,自定义业务逻辑,可以返回自定义的view或者模型数据。
当然,上面的鬼扯都是片面的,不代表行业的发展,只是博主管中窥豹而已。
下面步入正题,说说Spring MVC的最小化配置,给入门的朋友引个路。
Spring MVC的最小化配置
需要的jar包
- Spring framework spring-context
- Spring framework spring-mvc
具体可以参考maven中的引用:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
<!-- 默认是/WEB-INF/applicationContext.xml -->
</context-param> <listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringMVC-servlet.xml</param-value>
<!-- 默认是/WEB-INF/[servlet名字]-servlet.xml -->
</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>
其中,必要的配置就是指定servlet和listener.
- ContextLoaderListener指定了IOC容器初始化的方法
- DispatcherServlet则定义了mvc的相关内容,并配置拦截的url,如上面所示,所有/开头的请求,都会通过SpringMVC这个servlet进行处理。
他们都需要一个xml文件,默认位置上面已经说过了。
applicationContext.xml
空的,反正咱也没用什么bean。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> </beans>
SpringMVC-servlet.xml
里面放一个扫描controller的配置即可。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 设置使用注解的类所在的jar包 -->
<context:component-scan base-package="hello" />
</beans>
controller文件
package hello; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
public class HelloController { @RequestMapping("/hello")
public @ResponseBody String test() {
return "hello, world! This com from spring!";
} }
总结一下:
1 两个maven依赖,spring-context;spring-mvc。maven就会自动下载所有关联的jar包,包括
- spring-webmvc
- spring-beans
- spring-core
- spring-expression
- spring-web
- spring-context
- spring-aop
- aopalliance
- commons-logging
2 一个web.xml文件,配置了listener和servlet
3 两个spring相关的文件,applicationContext.xml和servletName-servlet.xml
4 一个controller文件,配置了拦截的url处理代码
有了这些准备工作,运行后输入
http://localhost:8080/项目地址/hello
就能得到
hello, world! This com from spring!
这样的信息,恭喜你的SpringMVC搭起来了!
https://www.cnblogs.com/xing901022/p/5240044.html
spirng: srping mvc配置(访问路径配置)搭建SpringMVC——最小化配置的更多相关文章
- 手把手教你搭建SpringMVC——最小化配置
为什么需要Spring MVC 最开始接触网页的时候,是纯的html/css页面,那个时候还是用Dreamweaver来绘制页面. 随着网站开发的深入,开始学习servlet开发,记得最痛苦的就是se ...
- spring:设置映射访问路径 或 xml配置访问路径 (spring mvc form表单)
项目hello, 在src/main/java下面建一个目录: charpter2 一.xml配置访问路径 web.xml <web-app> <display-name>Ar ...
- Spring boot 默认静态资源路径与手动配置访问路径
在application.propertis中配置 ##端口号server.port=8081 ##默认前缀spring.mvc.view.prefix=/## 响应页面默认后缀spring.mvc. ...
- SpringBoot系列三:SpringBoot基本概念(统一父 pom 管理、SpringBoot 代码测试、启动注解分析、配置访问路径、使用内置对象、项目打包发布)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.了解SpringBoot的基本概念 2.具体内容 在之前所建立的 SpringBoot 项目只是根据官方文档实现的一个基础程 ...
- Spring boot 默认静态资源路径与手动配置访问路径的方法
这篇文章主要介绍了Spring boot 默认静态资源路径与手动配置访问路径的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下 在application.propertis中配置 ##端口号 ...
- Servlet3.0注解配置访问路径和urlParttern配置
一.Servlet用注解配置访问路径 二.IDEA的tomcat相关配置 其中,第一点的配置文件,直接在IDEA的可视化操作界面修改就可以改掉配置文件中内容: 三.urlParttern配置 其中,* ...
- Git.之.最小化配置
Git.之.最小化配置 做一个全局的用户配置,便于以后提交代码等,记录当前操作的用户. ## 添加配置 # git config [--local | --global | --system] use ...
- springboot系列三、springboot 单元测试、配置访问路径、多个配置文件和多环境配置,项目打包发布
一.单元测试 生成的demo里面包含spring-boot-starter-test :测试模块,包括JUnit.Hamcrest.Mockito,没有的手动加上. <dependency> ...
- spring mvc 第一天【注解实现springmvc的基本配置】
创建pojo,添加标识类的注解@Controller,亦可添加该Handler的命名空间:设置类的@RequestMapping(value="/hr") 该类中的方法(Handl ...
随机推荐
- Taylor series
w用有限来表达无限,由已知到未知,化未知为已知. https://en.wikipedia.org/wiki/Taylor_series The Greek philosopher Zeno cons ...
- <2014 05 14> Android平台下2D/3D开发攻略
Android通过OpenGL包含了对高性能2D和3D图形的支持,尤其支持OpenGLES API.OpenGL是一个跨平台的图形API,提供了软件操作3D图形硬件的接口.OpenGLES是一个专用于 ...
- ES6通过WeakMap解决内存泄漏问题
一.Map 1.定义 Map对象保存键值对,类似于数据结构字典:与传统上的对象只能用字符串当键不同,Map对象可以使用任意值当键. 2.语法 new Map([iterable]) 属性 size:返 ...
- python多线程的适用场景
1.多线程对于计算密集型无用 需求:列表li1每个元素加1,列表li2每个元素加100 # 导入模块 import threading li1 = [11, 22, 33] # +1 li2 = [4 ...
- 快压、360压缩、WinRAR关于打开快压通过超高压缩比压缩后的文件不兼容的问题
今天接收了同事发过来的一个压缩文件,用360压缩打开和用WinRAR打开压缩文件,傻眼了,这发的是什么鬼压缩包.压缩包的文件大小有27533KB,用360压缩工具解压查看只有121.5kb,而且完全没 ...
- vim 设置 颜色值
编辑~/.vimrc文件,添加 set t_Co=8 t_Co即terminal Color之意 注意,将 t_Co 设置为256 (或8以外的所有值) 时,mark 的显示不是很正常.
- NIO服务端和客户端通信demo
代码转自 https://www.jianshu.com/p/a9d030fec081 服务端: package nio; import java.io.IOException; import jav ...
- hibernate validator 验证
@AssertTrue 用于boolean字段,该字段只能为true @AssertFalse 该字段的值只能为false @CreditCardNumber 对信用卡号进行一个大致的验证 @De ...
- HDU - 2819 Swap (二分图匹配-匈牙利算法)
题意:一个N*N的01矩阵,行与行.列与列之间可以互换.要求变换出一个对角线元素全为1的矩阵,给出互换的行号或列号. 分析:首先一个矩阵若能构成对角线元素全为1,那么矩阵的秩为N,秩小于N的情况无解. ...
- NGUI 3.50 UIButton使用
在NGUI,3.X的版本中,取消了创建UIbutton这个选项,所以我们可以创建uisprite.uilabel等,然后在上面附加uibutton脚本,达到目的,具体步骤 1:在界面上键好2D或3D ...