MVC的核心思想是业务数据抽取同业务数据呈现相分离。
MVC是一种架构模式,程序分层,分工合作,即相互独立又协同合作
DispatcherServlet就是一个前端控制器,Controller调用业务逻辑生成Model的地方,DispatcherServlet通过HandleMapping找到Controller和HandleInterceptor把他们形成了一个执行链条Handler,然后DispatcherServlet来调用,然后生成模型modelandview,再把模型返还给DispatcherServlet,DispatcherServlet调用ViewResolver视图解析器返还view对象,然后看到用户界面
mvc-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml
1
mvc-dispatcher
/
@RequestMapping请求映射
@RequestParam请求参数:/courses/view3?courseId=123
@PathVariable请求参数:/courses/view2/123
@ModelAttribute 对象绑定,jsp中的表单name和model保持一致
//本方法将处理 /courses/view?courseId=123 形式的URL
@RequestMapping(value="/view", method=RequestMethod.GET)
public String viewCourse(@RequestParam("courseId") Integer courseId,
Model model) {
log.debug("In viewCourse, courseId = {}", courseId);
Course course = courseService.getCoursebyId(courseId);
model.addAttribute(course);
return "course_overview";
}
//本方法将处理 /courses/view2/123 形式的URL
@RequestMapping("/view2/{courseId}")
public String viewCourse2(@PathVariable("courseId") Integer courseId,
Map model) {
log.debug("In viewCourse2, courseId = {}", courseId);
Course course = courseService.getCoursebyId(courseId);
model.put("course",course);
return "course_overview";
}
//本方法将处理 /courses/view3?courseId=123 形式的URL
@RequestMapping("/view3")
public String viewCourse3(HttpServletRequest request) {
Integer courseId = Integer.valueOf(request.getParameter("courseId"));
Course course = courseService.getCoursebyId(courseId);
request.setAttribute("course",course);
return "course_overview";
}
@RequestMapping(value="/admin", method = RequestMethod.GET, params = "add")
public String createCourse(){
return "course_admin/edit";
}
@RequestMapping(value="/save", method = RequestMethod.POST)
public String doSave(@ModelAttribute Course course){
log.debug("Info of Course:");
log.debug(ReflectionToStringBuilder.toString(course));
//在此进行业务操作,比如数据库持久化
course.setCourseId(123);
return "redirect:view2/"+course.getCourseId();
}
配置文件解析、增加相关依赖
commons-fileupload
commons-fileupload
1.3.1
使用
@RequestMapping(value="/doUpload", method=RequestMethod.POST)
public String doUploadFile(@RequestParam("file") MultipartFile file) throws IOException{
if(!file.isEmpty()){
log.debug("Process file: {}", file.getOriginalFilename());
FileUtils.copyInputStreamToFile(file.getInputStream(), new File("c:\\temp\\imooc\\", System.currentTimeMillis()+ file.getOriginalFilename()));
}
return "success";
}
配置文件解析、增加相关依赖
com.fasterxml.jackson.core
jackson-databind
${jackson.version}
使用
@RequestMapping(value="/{courseId}",method=RequestMethod.GET)
public @ResponseBody Course getCourseInJson(@PathVariable Integer courseId){
return courseService.getCoursebyId(courseId);
}
@RequestMapping(value="/jsontype/{courseId}",method=RequestMethod.GET)
public ResponseEntity getCourseInJson2(@PathVariable Integer courseId){
Course course = courseService.getCoursebyId(courseId);
return new ResponseEntity(course, HttpStatus.OK);
}
是拦截器指通过统一拦截从浏览器发往服务器的请求来完成功能的增强
使用场景:解决请求的共性问题,如:乱码问题、权限验证问题等
工作原理和过滤器相似,可以用来解决乱码问题
Spring MVC默认提供了CharacterEncodingFilter过滤器类,来对客户端的请求进行编码设置
多个拦截器时,拦截器方法的执行顺序