Spring MVC最简单的配置

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

  1. 在web.xml中配置Servlet;
  2. 创建Spring MVC的xml配置文件;
  3. 创建Controller和View
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. version="3.1">
  6.  
  7. <!-- spring mvc配置开始 -->
  8. <servlet>
  9. <servlet-name>test</servlet-name>
  10. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  11. <load-on-startup>1</load-on-startup>
  12. </servlet>
  13.  
  14. <servlet-mapping>
  15. <servlet-name>test</servlet-name>
  16. <url-pattern>/</url-pattern>
  17. </servlet-mapping>
  18. <!-- spring mvc配置结束 -->
  19.  
  20. <welcome-file-list>
  21. <welcome-file>index</welcome-file>
  22. </welcome-file-list>
  23. </web-app>

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

servlet.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  8. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  9.  
  10. <mvc:annotation-driven/>
  11. <context:component-scan base-package="com.excelib" />
  12. </beans>

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

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

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

若果没有配置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. traceroute tracert 路由器地址 清单 192.168.2.1 网关路由器地址

    [root@a ~]# traceroute www.ijntv.cntraceroute to www.ijntv.cn (42.81.61.31), 30 hops max, 60 byte pa ...

  2. linux mint19.1解决网易云音乐安装后打不开的问题

    安装网易云音乐: sudo dpkg -i 文件路径#文件路径可以直接把刚才下载的软件包拖进终端sudo apt install -f 修复依赖关系 安装后打不开的问题: 1.sudo gedit / ...

  3. golang环境安装

    到官方https://golang.org/dl/下载安装包 cd /usr/local/src wget https://storage.googleapis.com/golang/go1.8.li ...

  4. C++应该被看成是个语言集合——四种语言(C语言,OO语言,泛型语言,STL)

    至少有三种语言: 一,C++ is C 二,C++ is an OO language 三,C++ is a genetic programming language 有的童鞋觉得难,可能是没有看清楚 ...

  5. Cyclic Nacklace ---hdu3746(循环节,kmp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 给你一个字符串,让你在后面加尽量少的字符,使得这个字符串成为一个重复串. abca---添加bc ...

  6. 一.shell基础知识

    参考网站:http://billie66.github.io/TLCL/book/chap08.html 1.字符“*”--展开 [me@linuxbox ~]$ echo * Desktop Doc ...

  7. 完全用nosql轻松打造千万级数据量的微博系统

    其实微博是一个结构相对简单,但数据量却是很庞大的一种产品.标题所说的是千万级数据量也并不是一千万条微博信息而已,而是千万级订阅关系之间发布.在看 我这篇文章之前,大多数人都看过sina的杨卫华大牛的微 ...

  8. git学习------>如何汉化GitLab?

    在上一篇博客中,已经正常安装好了GitLab,然而全部界面都是纯英文的,为了照顾整个团队的英文水平,因此这篇博客的目的是将纯英文的GitLab进行汉化. 纯英文界面 第一步: 确认GitLab版本号 ...

  9. 使用python操作json文本文件

    使用python读写文本文件内容时,我们知道如果文本文件里的内容无规律,那么修改起来比较麻烦.但是如果文本文件存储是有规律的,比如JSON格式,在利用python内置的函数把JSON格式的数据转成py ...

  10. 【基础算法】- 个人认为最快的 Fibonacci 程序

    public class Fibonacci { private static Map<Long,Long> map = new HashMap<Long,Long>(); s ...