spring实战第四版读书笔记-第五章 spring构建web应用

[TOC]

#构建Spring Web应用程序

Spring MVC

Spring MVC 请求流程

  1. 请求—> org.springframework.web.servlet.DispatcherServlet
  2. DispatcherServlet处理请求
  3. DispatcherServlet —>控制器
  4. 控制器 —> 模型及逻辑视图名
  5. 模型及逻辑视图名 —> DispatcherServlet
  6. DispatcherServlet —>视图解析器
  7. 响应视图 —> 用户

路径变量

1
2
3
4
5
6
7
8
(method = RequestMethod.GET, value="/blog/{id}")
public ModelAndView (@PathVariable("id") Long id) throws Exception
{
ModelAndView modelAndView = new ModelAndView(JspConstant.BLOG_ITEM);
BlogArticle article = mBlogArticleService.getBy(id);
modelAndView.addObject(ATTR_NAME_ARTICLE, article);
return modelAndView;
}

参数验证

1
2
3
4
5
6
7
8
9
10
11
12
(method = RequestMethod.POST, value="/blogAdd.do")
public ModelAndView blogAdd(HttpServletRequest request,
@Valid BlogArticle article,
Errors errors) throws Exception
{
if(errors.hasErrors()){
return new ModelAndView("blog_add");
}
mBlogArticleService.addArticle(article);
ModelAndView modelAndView = new ModelAndView("redirect:/indexBlog.do");
return modelAndView;
}