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 复制代码