Spring MVC最简单的配置

  配置一个Spring MVC只需要三步:

  1. 在web.xml中配置Servlet;
  2. 创建Spring MVC的xml配置文件;
  3. 创建Controller和View
 <?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="3.1"> <!-- spring mvc配置开始 -->
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- spring mvc配置结束 --> <welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
</web-app>

所配置的Servlet是DispatcherServlet类型,它就是Spring MVC的入口,Spring MVC的本质就是一个Servlet。在配置DispatcherServlet的时候可以设置contextConfigLocation参数来指定Spring MVC配置文件的位置,如果不指定就默认使用WEB-INF/[ServletName]-servlet.xml文件。

servlet.xml

 <?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven/>
<context:component-scan base-package="com.excelib" />
</beans>

<mvc:annotation-driven/>是Spring MVC提供的一键式的配置方法,配置此标签后Spring MVC会自动做一些注册组件之类的事情。

context:component-scan base-package="com.excelib" />扫描通过注释配置的类,还可以通过context:include-filter子标签来设置只扫描@Controller就可以了,别的交给Spring容器管理

 <context:component-scan base-package="com.excelib" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
 @Controller
public class GoController {
private final Log logger = LogFactory.getLog(GoController.class);
//处理HEAD类型的”/”请求,HEAD类型的请求可以用来检测服务器的状态,因为它不返回body所以比GET请求更省网络资源
@RequestMapping(value={"/"},method= {RequestMethod.HEAD})
public String head() {
return "go.jsp";
}
//处理GET类型的"/index"和”/”请求
@RequestMapping(value={"/index","/"},method= {RequestMethod.GET})
public String index(Model model) throws Exception {
logger.info("======processed by index=======");
//返回msg参数
model.addAttribute("msg", "Go Go Go!");
return "go.jsp";
}
}

若果没有配置ViewResolver,Spring MVC将默认使用org.springframework.web.servlet.view.InternalResourceViewResolver作为ViewResolver。

spring mvc 之初体验的更多相关文章

  1. Spring MVC开发初体验

    1.目标实现Spring MVC : Hello World! 2.工程创建步骤 new : Dynamic Web Project lib引入Spring框架libs/*.jar touch web ...

  2. Spring boot缓存初体验

    spring boot缓存初体验 1.项目搭建 使用MySQL作为数据库,spring boot集成mybatis来操作数据库,所以在使用springboot的cache组件时,需要先搭建一个简单的s ...

  3. 215.Spring Boot+Spring Security:初体验

    [视频&交流平台] SpringBoot视频:http://t.cn/R3QepWG Spring Cloud视频:http://t.cn/R3QeRZc SpringBoot Shiro视频 ...

  4. Spring MVC学习初篇

    Spring mvc 使用配置: <!-- 使用MVC --> <servlet> <servlet-name>defaultDispatcher</serv ...

  5. Spring For Android初体验

    Spring For Android是Spring框架的一个扩展,其主要目的在乎简化Android本地应用的开发,这其中包括了你可以使用该项目提供的 RestTemplate来为你的Android客户 ...

  6. Spring Cloud Alibaba 初体验(六) Seata 及结合 MyBatis 与 MyBatis-Plus 的使用

    一.下载与运行 本文使用 Seata 1.1.0:https://github.com/seata/seata/releases Windows 环境下双击 bin/seata-server.bat ...

  7. MVC + AngularJS 初体验(实现表单操作)

    AngularJS AngularJS 通过新的属性和表达式扩展了 HTML. AngularJS 可以构建一个单一页面应用程序(SPAs:Single Page Applications). Ang ...

  8. Spring Data JPA 初体验

    一,JPA相关的概念 JPA概述 全称是:JavaPersistence API.是SUN公司推出的一套基于ORM的规范. Hibernate框架中提供了JPA的实现. JPA通过JDK 5.0注解或 ...

  9. asp.net mvc 5 初体验

    参考:http://www.asp.net/mvc/tutorials/mvc-5/introduction/getting-started 1. 新建 ASP.Net Web 应用程序,跟着向导一路 ...

随机推荐

  1. Apache Kafka源码分析 – Log Management

    LogManager LogManager会管理broker上所有的logs(在一个log目录下),一个topic的一个partition对应于一个log(一个log子目录)首先loadLogs会加载 ...

  2. vue项目中批量打印二维码

    前提:项目中要打印的二维码为后台返回,批量选择后,点击打印,先打开二维码预览界面,再执行打印. 以下代码中 codePicList为选中的二维码数组.重点css:page-break-after:al ...

  3. PHP 错误日志

    display_errors 错误回显,一般常用语开发模式,但是很多应用在正式环境中也忘记了关闭此选项.错误回显可以暴露出非常多的敏感信息,为攻击者下一步攻击提供便利.推荐关闭此选项. display ...

  4. 并发编程 - 协程 - 1.协程概念/2.greenlet模块/3.gevent模块/4.gevent实现并发的套接字通信

    1.协程并发:切+保存状态单线程下实现并发:协程 切+ 保存状态 yield 遇到io切,提高效率 遇到计算切,并没有提高效率 检测单线程下 IO行为 io阻塞 切 相当于骗操作系统 一直处于计算协程 ...

  5. LRU算法的Python实现

    http://flychao88.iteye.com/blog/1977653文章中介绍了常见的几种缓存淘汰策略 LRU:least recently used,最近最少使用算法.其实就是按使用时间倒 ...

  6. Ubuntu安装Python3 和卸载

    Python2中文的解决 在py文件第一行添加 #coding=utf-8 1 规范的应该这么写 #-*- coding:utf-8 -*- 1 安装python 系统默认安装Python2 安装Py ...

  7. MFC Spin 控件

    一般应用: 设置属性: Auto Buddy(自动取关联控件为TAB顺序前一个)Set Buddy Interger(使控件设置关联控件数值,这个值可以是十进制或十六进制)Wrap(数值超过范围时循环 ...

  8. mysql的相关信息

    mysql数据库优化技巧 表的设计合理(字段合理,符合3NF) 添加适当索引(index)[主要4种:普通索引,主键索引,唯一索引unique,全文索引] 分表技术(水平分表->拆分表结构,垂直 ...

  9. JS通过身份证号获取生日、年龄、性别

    <script> function IdCard(UUserCard,num){ if(num==1){ //获取出生日期 birth=UUserCard.substring(6, 10) ...

  10. Mediakit报告设备商的空间不足以执行此操作的纯MAC解法

    使用Mac对磁盘进行分区,显示“Mediakit报告设备商的空间不足以执行此操作”,该怎么办? What 买了一个4TB的移动硬盘,准备进行分区给Time Machine用. 硬盘自带是HDFS的,所 ...