SpringMVC:也叫Spring web mvc,属于表现层框架。SpringMVC是Spring框架的一部分,是在Spring3.0后发布的。
由以上Spring的结构图可以看出,Spring由四大部分组成:Dao部分(DAO与ORM)、AOP部分、Web部分(JEE与Web),及loC容器部分(Core)。
1.需要导入的jar包
Spring-context-support:包含支持UI模板,邮件服务,缓存Cache等方面的类。
Spring-webmvc:对SpringMVC的实现。
Spring Frame 4.3.8源码下载地址:https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring/4.3.8.RELEASE/spring-framework-4.3.8.RELEASE-dist.zip
2.创建一个controller类,实现Controller接口
public class MyController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
ModelAndView mv = new ModelAndView();
//其底层执行的是request.setAttribute()方法
mv.addObject("message", "Hello SpringMVC world!");
mv.setViewName("welcome");
return mv;
}
}
这里是随意写的类,需要配置注册才能使其生效。=>在spring配置文件中注册。
3.SpringMCV配置
- 添加约束
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns:tx="http://www.springframework.org/schema/