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. linux下非root用户的sudo问题

    linux下的root用户是个超级管理员,一般是不用这个用户登录进行操作的,但有时候需要root权限,又不想切换用户的话可以使用sudo命令.但是不是所有的用户都可以使用sudo命令的. 首先可能会遇 ...

  2. C++的全部目标就是最优化资源的利用,以人付出更多为代价。Python刚好是另一个极端(Bjarne就说,一个人至少应该掌握两种计算机语言)

    说 C++ 反人类,是如果把 C++ 看作人(程序员)和资源(电子系统)的桥梁,他的全部目标就是最优化资源的利用,以人付出更多为代价.Python刚好是另一个极端.做好两个一起学.Bjarne就说,一 ...

  3. 使用LoadRunner的Web(HTTP/HTML)协议录制手机app脚本

    一.打开HP Virtual User Generator,创建虚拟用户脚本,选择Web(HTTP/HTML)协议:

  4. Incorrect key file for table ' '; try to repair it

    场景:为有150W的数据表增加字段时,报错 解决:在my.ini配置临时目录configure tmpdir. Where MySQL Stores Temporary Files

  5. Linux环境下Netstat与PS的使用

    Linux下用netstat查看网络状态.端口状态 在linux一般使用netstat 来查看系统端口使用情况步. netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表.实际 ...

  6. es6的Set和Map数据结构

    Set 和 Map 数据结构 Set WeakSet Map WeakMap Set § ⇧ 基本用法 ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set ...

  7. Akka in action (第一章 介绍Akka)

    在本章 概述Akka 了解Actors和Actor系统 Akka的适用范围 在第一章中,会介绍给你Akk的个方面,它能做什么,与现有的解决方案有那些不同.重点关注Akka有哪些功能和使用范围和强大的并 ...

  8. 设置eclipse编码格式

    1.修改eclipse默认工作空间编码方式.点击Window-->Preferences-->General-->Workspace,设置编码格式为UTF-8,然后点击OK.

  9. ansible playbook部署ELK集群系统

    一.介绍 总共4台机器,分别为 192.168.1.99 192.168.1.100 192.168.1.210 192.168.1.211 服务所在机器为: redis:192.168.1.211 ...

  10. $python用装饰器实现一个计时器

    直接上代码: import time from functools import wraps # 定义装饰器 def fn_timer(function): @wraps(function) def ...