前言
感谢! 承蒙关照~
Java
中的Spring MVC
简介笔记
MVC
简介
Spring MVC
基本概念Spring MVC
项目搭建
maven
使用
Spring MVC
进行开发
实现数据绑定和文件上传
MVC
mvc
是开发web
架构的通用方式.
Front Controller(MVC)
前端控制器,Front controller
到Controller
到View template
,用户页面.
前端控制器
前端控制器用于分发调度,而控制器用于业务数据抽取,view template
用于页面呈现.
MVC
的本质
mvc
的本质是将业务数据的抽取与业务逻辑的呈现想分离.(数据和呈现分离).
什么是所谓的mvc
mvc
- Model-View-Controller
程序中的view
是视图层,用于提供ui
,是用来呈现数据的.模型层是业务数据的信息的表示.
模型层:
// 业务信息的构成
public class Course {
private Integer courseId;
private String title;
private Integer learningNum;
private Integer level;
private String imgPath;
private List<Chapter> chapterList;
public Integer getCourseId(){
return courseId;
}
public void setCourseId(Integer courseId){
this.courseId = courseId;
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
public Integer getLearningNum(){
return learningNum;
}
}
复制代码
视图和模型- Controller
控制器,控制层,作用是将数据传递给视图层,用于呈现.
mvc
是一种架构模式,用于呈现的分层,分工合作,模型层,视图层,控制层.
spring mvc
概念:
DispatcherServlet
controller
- HandlerAdapter
表现形式
集合
DispatcherServlet
Controller
HandlerAdapter
HandlerInterceptor
HandlerMapping
HandlerExecutionChain
ModelAndView
ViewResolver
View
复制代码
DispatcherServlet
重点
View
Controller
DispatcherServlet
-> Request
-> HandlerMapping
-> Controller
-> HandlerInterceptor
-> Hander/HandlerAdapter
ModelAndView
-> DispatcherServlet
-> ViewResolver
-> View
-> Response
结构
DispatcherServlet
Common Services
->
Locale Resolution
Theme Resolution
Multipart(File Upload) Handing
复制代码
Controller
HandlerMapping
HandlerInterceptor
Handler
-Data Binding
-Validation, Type Conversion, Formatting
-Invoke Service Layer
-Update Model and State
HandlerExceptionResolver
复制代码
View
ViewResolver
View
复制代码
spring mvc
项目搭建-maven
// maven
pom(project object model) - an xml file(pom.xml)
// contains information
dependency management
coordinates
复制代码
坐标Coordinates
groupId
artifactId
version
packaging
复制代码
maven
安装
下载解压maven
,配置环境变量,(M2_HOME,Path
),配置maven
配置文件.
(M2_HOME,Path)
配置文件:
%HOME%\.m2
复制代码
复制settings.xml
文件到.m2
下
打开.m2
下文件settings.cml
文件
配置:
<mirror>
<id>UK</id>
<name>UK Central</name>
<url>http://uk.maven.org/maven2</url>
<mirrorOf>central</mirrorOf>
</mirror>
复制代码
maven
创建项目
eclipse
-> eclipse kepler sr2 packages
eclipse
-> window
-> preferences
-> maven
-> Installations
-> add
-> 'maven-
apache-maven-x.x.x->
user settings`中确保路径正确.
mvn archetype:generate -DgroupId=dashucoding-arthur -DartifactId=spring-mvc-study
-DarchetypeArtifactId=maven-archetype-webapp
复制代码
点击回车键-都回车键
项目
Import
-> maven
-> existion maven projects
-> spring-mvc-study
/pom.xml
spring-mvc-study
-> properties
-> java build path
-> spring-mvc-study/src/main/java(missing)
main
-> folder
-> java
-> pom.xml
packaging: jar
复制代码
点击pom.xml
@Controller
@RequestMapping("/hello")
public class HelloMvcController{
@RequestMapping("/mvc");
// host:8080/hello/mvc
public String helloMvc(){
return "home";
}
}
复制代码
mvn jetty:run
复制代码
spring mvc
操作
DispatcherServlet
WebApplicationContext
WebApplication
复制代码
controller
model
service
复制代码
@Controller
@RequestMapping("/courses")
public class CourseController {
private static Logger log = LoggerFactory.getLogger(CourseController.class);
private CourseService courseService;
}
复制代码
Controller
@Controller
@RequestMapping
URL template(@RequestParam and @PathVarialbe)
HttpServletRequest and / or HttpSession
复制代码
Binding
绑定
是将请求中的字段按照名字匹配的原则填入模型对象.
CourseController.java
@RequestMapping(value="/admin", method=RequestMethod.GET, params="add")
public String createCourse(){
return "course_admin/edit";
}
复制代码
package com.model;
复制代码
FileUpload
单文件上传
文件上传:
<dependency>
<groupId> commons-fileupload </groupId>
<artifactId> commons-fileupload </artifactId>
<version>1.3.1</version>
</dependency>
复制代码
JSON
json是一种轻量级的格式
复制代码
ContentNegotiatingViewResolver
ResponseEntity
@ResponseBody/@ResquestBody
复制代码
知识点
@RequestMapping
@PathVariable
@Controller
redirect/forward
MultipartFile
@ModelAttribute
@ResquestBody
@RequestParam
@ResponseBody
enctype="multipart/form-data"
multipartResolver
ResponseEntity
复制代码
达叔小生:往后余生,唯独有你 You and me, we are family ! 90后帅气小伙,良好的开发习惯;独立思考的能力;主动并且善于沟通 简书博客: 达叔小生 www.jianshu.com/u/c785ece60…
结语
- 下面我将继续对 其他知识 深入讲解 ,有兴趣可以继续关注
- 小礼物走一走 or 点赞