目录
什么是spring MVC?
流程图:
dispatcherServlet相关方法:
代码下载:https://gitee.com/hong99/spring/issues/I1N1DF
代码实现
maven相关依赖
配置简单mvc
关于:@PathVariable的应用
结果
Spring 异常处理
最后
MVC 是一种架构思想,指的是model(模型)-view(视图)-controller(控制器),spring mvc将各层解耦,每一层都负责对应的功能和职责,将原本耦合在一起的逻辑进行解耦。而spring MVC就是利用该架构思想将web进行解耦。
1.用户发起请求调用dispatcherServlet;
2.dispatcherServlet通过initstrategies初始化工作,然后通过HandlerMapping查找匹配的hanler;
3.接着获取到HandlerAdapter适合配器,再通过HandlerAdapter获取合适的Handler;
4.通过Handler获取ModelAndView再返回视图解析并且返回一个controller
5.再通过controller(控制器)返回model和view给用户;
/**
*初始化方法
*/
protected void initStrategies(ApplicationContext context) {
//初始化上传文件解析器
initMultipartResolver(context);
//初始化本地化解析器
initLocaleResolver(context);
//初始化主题解析器
initThemeResolver(context);
//初始化处理器映射器
initHandlerMappings(context);
//初始化适配器
initHandlerAdapters(context);
//初始化异常解析器
initHandlerExceptionResolvers(context);
//初始化请求到视图名翻译器
initRequestToViewNameTranslator(context);
//初始化视图解析器
initViewResolvers(context);
//初始化重定向数据管理器
initFlashMapManager(context);
}
org.springframework
spring-beans
4.3.11.RELEASE
org.springframework
spring-context
4.3.11.RELEASE
org.springframework
spring-context-support
4.3.11.RELEASE
org.springframework
spring-expression
4.3.11.RELEASE
org.springframework
spring-core
4.3.11.RELEASE
org.springframework
spring-web
4.3.11.RELEASE
org.springframework
spring-webmvc
4.3.11.RELEASE
org.springframework
spring-test
4.3.11.RELEASE
test
org.springframework
spring-tx
4.3.11.RELEASE
junit
junit
4.13
test
org.springframework
spring-test
4.3.11.RELEASE
com.alibaba
fastjson
1.2.70
org.springframework
spring-aop
4.3.11.RELEASE
org.aspectj
aspectjrt
1.9.5
org.aspectj
aspectjweaver
1.9.5
runtime
org.aspectj
aspectjtools
1.9.5
org.springframework
spring-asm
javax.servlet
javax.servlet-api
4.0.1
javax.servlet.jsp.jstl
jstl
1.2
项目图
web.xml
Spring MVC Application
HelloWeb
org.springframework.web.servlet.DispatcherServlet
1
HelloWeb
/
HelloWeb-servlet.xml
home.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Hello Spring MVC
${message}
@Controller
@RequestMapping("/")
public class HomeController {
@RequestMapping(value = "home",method = RequestMethod.GET)
public String home(ModelMap model) {
model.addAttribute("message","Hello home");
return "home";
}
}
部署方式
结果
student.jsp
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Spring MVC Form Handling
Student Information
Name
Age
id
result.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Spring MVC Form Handling
Submitted Student Information
Name
${name}
Age
${age}
ID
${id}
@Controller
public class StudentController {
@RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView("student", "command", new Student());
}
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(@ModelAttribute("SpringWeb")Student student,
ModelMap model) {
model.addAttribute("name", student.getName());
model.addAttribute("age", student.getAge());
model.addAttribute("id", student.getId());
return "result";
}
}
/**
* @Auther: csh
* @Date: 2020/7/23 09:52
* @Description:该控制器主要用于 @PathVaiable 学习
*/
@Controller
public class PathVariableController {
/**
*
* 功能描述:通过传入值获取
*
* @param:
* @return:
* @auther: csh
* @date: 2020/7/26 9:39
*/
@RequestMapping("/{id}")
public String showUserInfo(ModelMap modelMap, @PathVariable("id")Integer id){
Student student = new Student();
student.setId(id);
student.setAge(100);
student.setName("test");
modelMap.addAttribute("name", student.getName());
modelMap.addAttribute("age", student.getAge());
modelMap.addAttribute("id", student.getId());
return "result";
}
/**
*
* 功能描述:跳转首页
*
* @param:
* @return:
* @auther: csh
* @date: 2020/7/26 9:39
*/
@RequestMapping("/redirect")
public String redirect(){
return "redirect:home";
}
}
/**
* @Auther: csh
* @Date: 2020/7/26 09:46
* @Description:spring异常
*/
public class SpringException extends RuntimeException {
private String exceptionMsg;
public SpringException(String exceptionMsg) {
this.exceptionMsg = exceptionMsg;
}
public String getExceptionMsg(){
return this.exceptionMsg;
}
public void setExceptionMsg(String exceptionMsg) {
this.exceptionMsg = exceptionMsg;
}
}
/**
* @Auther: csh
* @Date: 2020/7/26 09:47
* @Description:该类主要是测试spring异常
*/
@Controller
public class ExceptionControler {
@RequestMapping(value = "/exception",method = RequestMethod.GET)
@ExceptionHandler({SpringException.class})
public String exception(){
int i =0;
if(i==0){
throw new SpringException("出错了!");
}
return "result";
}
}
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Spring Error Page
${exception.exceptionMsg}
HelloWeb-servlet.xml
error
spring mvc相对来说理解起来很好理解,毕竟走ee方向的老铁都基本会接触到,但是复杂在于里面的底层实现原理,相对来说的确具体的逻辑的和实现不好理解,本文也是非常浅的带过,所用的jsp,其实在企业中都已经废弃到,只是为了更好的理解所以才加入,后续再统一深入了解。
参考文章:
流程说明:https://blog.csdn.net/zuoluoboy/article/details/19766131
https://blog.csdn.net/qq_37561309/article/details/106825088