spring mvc 之初体验
Spring MVC最简单的配置
配置一个Spring MVC只需要三步:
- 在web.xml中配置Servlet;
- 创建Spring MVC的xml配置文件;
- 创建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 之初体验的更多相关文章
- Spring MVC开发初体验
1.目标实现Spring MVC : Hello World! 2.工程创建步骤 new : Dynamic Web Project lib引入Spring框架libs/*.jar touch web ...
- Spring boot缓存初体验
spring boot缓存初体验 1.项目搭建 使用MySQL作为数据库,spring boot集成mybatis来操作数据库,所以在使用springboot的cache组件时,需要先搭建一个简单的s ...
- 215.Spring Boot+Spring Security:初体验
[视频&交流平台] SpringBoot视频:http://t.cn/R3QepWG Spring Cloud视频:http://t.cn/R3QeRZc SpringBoot Shiro视频 ...
- Spring MVC学习初篇
Spring mvc 使用配置: <!-- 使用MVC --> <servlet> <servlet-name>defaultDispatcher</serv ...
- Spring For Android初体验
Spring For Android是Spring框架的一个扩展,其主要目的在乎简化Android本地应用的开发,这其中包括了你可以使用该项目提供的 RestTemplate来为你的Android客户 ...
- Spring Cloud Alibaba 初体验(六) Seata 及结合 MyBatis 与 MyBatis-Plus 的使用
一.下载与运行 本文使用 Seata 1.1.0:https://github.com/seata/seata/releases Windows 环境下双击 bin/seata-server.bat ...
- MVC + AngularJS 初体验(实现表单操作)
AngularJS AngularJS 通过新的属性和表达式扩展了 HTML. AngularJS 可以构建一个单一页面应用程序(SPAs:Single Page Applications). Ang ...
- Spring Data JPA 初体验
一,JPA相关的概念 JPA概述 全称是:JavaPersistence API.是SUN公司推出的一套基于ORM的规范. Hibernate框架中提供了JPA的实现. JPA通过JDK 5.0注解或 ...
- asp.net mvc 5 初体验
参考:http://www.asp.net/mvc/tutorials/mvc-5/introduction/getting-started 1. 新建 ASP.Net Web 应用程序,跟着向导一路 ...
随机推荐
- 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 ...
- linux mint19.1解决网易云音乐安装后打不开的问题
安装网易云音乐: sudo dpkg -i 文件路径#文件路径可以直接把刚才下载的软件包拖进终端sudo apt install -f 修复依赖关系 安装后打不开的问题: 1.sudo gedit / ...
- golang环境安装
到官方https://golang.org/dl/下载安装包 cd /usr/local/src wget https://storage.googleapis.com/golang/go1.8.li ...
- C++应该被看成是个语言集合——四种语言(C语言,OO语言,泛型语言,STL)
至少有三种语言: 一,C++ is C 二,C++ is an OO language 三,C++ is a genetic programming language 有的童鞋觉得难,可能是没有看清楚 ...
- Cyclic Nacklace ---hdu3746(循环节,kmp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 给你一个字符串,让你在后面加尽量少的字符,使得这个字符串成为一个重复串. abca---添加bc ...
- 一.shell基础知识
参考网站:http://billie66.github.io/TLCL/book/chap08.html 1.字符“*”--展开 [me@linuxbox ~]$ echo * Desktop Doc ...
- 完全用nosql轻松打造千万级数据量的微博系统
其实微博是一个结构相对简单,但数据量却是很庞大的一种产品.标题所说的是千万级数据量也并不是一千万条微博信息而已,而是千万级订阅关系之间发布.在看 我这篇文章之前,大多数人都看过sina的杨卫华大牛的微 ...
- git学习------>如何汉化GitLab?
在上一篇博客中,已经正常安装好了GitLab,然而全部界面都是纯英文的,为了照顾整个团队的英文水平,因此这篇博客的目的是将纯英文的GitLab进行汉化. 纯英文界面 第一步: 确认GitLab版本号 ...
- 使用python操作json文本文件
使用python读写文本文件内容时,我们知道如果文本文件里的内容无规律,那么修改起来比较麻烦.但是如果文本文件存储是有规律的,比如JSON格式,在利用python内置的函数把JSON格式的数据转成py ...
- 【基础算法】- 个人认为最快的 Fibonacci 程序
public class Fibonacci { private static Map<Long,Long> map = new HashMap<Long,Long>(); s ...