01_SpringMVC入门
张创琦
基础文件
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.kkb</groupId><artifactId>springmvc01</artifactId><version>1.0-SNAPSHOT</version><!-- web项目,需要引入war包 --><packaging>war</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.13.RELEASE</version></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version><!-- 不会运行上去 --><scope>provided</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.0</version><configuration><target>1.8</target><source>1.8</source></configuration></plugin><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.2</version><configuration><path>/</path><port>80</port></configuration></plugin></plugins></build><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties></project>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- spring配置文件,除了控制器之外的bean对象都在这里扫描, model层 --><context:component-scan base-package="com.kkb.dao, com.kkb.service"/></beans>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- springmvc的配置文件,控制器的bean对象都在这里扫描 --><context:component-scan base-package="com.kkb.controller"/><!--注解驱动--><mvc:annotation-driven/><!--视图解析器--><bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><!--前缀--><property name="prefix" value="/jsp/"></property><!--后缀--><property name="suffix" value=".jsp"></property></bean></beans>
TeamService.java
package com.kkb.service;import org.springframework.stereotype.Service;@Servicepublic class TeamService {public void add(){System.out.println("TeamService-----add--------");}}
TeamController.java
package com.kkb.controller;import com.kkb.service.TeamService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@Controllerpublic class TeamController {@Autowiredpublic TeamService teamService;// /hello.do也可@RequestMapping("hello.do")public ModelAndView add(){System.out.println("TeamController------add------");teamService.add();ModelAndView mv = new ModelAndView();mv.addObject("teamName", "湖人队"); //相当于Request.setAttribute("","")mv.setViewName("index"); //未来通过springmvc的视图解析器处理。转换成物理资源路径,相当于request.getRequestDispatcher("index.jsp").forward();//经过InternalResourceViewResolver对象的处理之后加上前后缀就变成了 /jsp/index.jspreturn mv;}}
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><!-- Spring配置 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><!-- ContextLoaderListener默认监听WEB-INF下面的applicationContext.xml文件,所以上面的spring配置必须写 --><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- springmvc前端/核心/中央控制器 --><servlet><!-- 如果将servlet-name改为springmvc的话,那下面的mapping映射里的servlet-name也要改为springmvc,此时param-name找的是springmvc-servlet.xml --><servlet-name>dispatcherServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name> <!-- 会去找dispatcherServlet-servlet.xml --><param-value>classpath:springmvc.xml</param-value></init-param><!--没有写的话现用现创建,大于0的话表示立即创建对象,数字越小优先级越高,小于0的话不会立即创建对象 --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcherServlet</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping></web-app>
index.jsp
<%--Created by IntelliJ IDEA.User: DELLDate: 2022/2/12Time: 20:41To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><title>index</title></head><body><h1>index----------${teamName}</h1></body></html>
说明:
1 整理创建web项目的流程(视频:SpringMVC入门程序)
2 JDK选择1.8
3 spring和springmvc版本号统一
5.2.13.RELEASE
4 springmvc的底层就是对servlet的底层封装
5 前端发送的请求将会去springmvc查找
6 方便配置文件的管理
