SpringMVC简介


1.导入spring-mvc坐标
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-webmvcartifactId>
- <version>5.3.7version>
- dependency>
2.在web.xml里配置DispatcherServlet前端控制器
- <servlet>
- <servlet-name>DispatcherServletservlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
- <init-param>
- <param-name>contextConfigLocationnparam-name>
- <param-value>classpath:spring-mvc.xmlparam-value>
- init-param>
- <load-on-startup>2load-on-startup>
- servlet>
3.编写controller(注入了spring容器里的servise)
- @Controller
- public class QuickController {
- //直接注入service进行使用
- @Autowired
- private QuickService quickService;
- @RequestMapping("/show")
- public String show(){
- System.out.println("show running..."+quickService);
- return "/index.jsp";
- }
-
- }
3.5 spring-mvc容器组件扫描
-
- <context:component-scan base-package="com.itheima.controller"/>
3.5.5 返回页面index.jsp
- <%--
- Created by IntelliJ IDEA.
- User: haohao
- Date: 2022/12/1
- Time: 16:04
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Titletitle>
- head>
- <body>
- <h1>Hello SpringMVC!h1>
-
- body>
- html>
spring容器仍然为:service+application.xml模式