入门案例
案例实现
步骤1
- 导入坐标
<?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>org.example</groupId>
<artifactId>SpringMVC_text</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!-- spring 的坐标 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- spring web 的坐标 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!-- spring mvc 的坐标 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
步骤2
- 定义表现层业务处理器controller,并配置成spring的bean(等同于servlet)
package com.liu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
public String save(){
System.out.println("user mvc controller is running ...... ");
}
}
- 该bean的处理需要使用独立的配置文件扫描(XML版)(
spring-mvc.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.liu" />
</beans>
步骤3
- web .xml中配置springMvc核心控制器,用于将请求转发到对应的具体业务处理器controller中(等同于servlet配置)
<?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">
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
步骤4
- 设定具体controller的访问路径(等同于servlet在web. xml中的配置)
package com.liu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("/save")
public String save(){
System.out.println("user mvc controller is running ...... ");
}
}
步骤5
- 设置返回页面
package com.liu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController {
@RequestMapping("/save")
public String save(){
System.out.println("user mvc controller is running ...... ");
return "success.jsp";
}
}
入门案例工作流程分析
- 服务器启动
- 加载web.xrn1中Dispatcherservlet
- 读取spring-mvc.xml中的配置,加载所有com.itheima包中所有标记为bean的类
- 读取bean中方法上方标注@RecuestMapping的内容
- 处理请求
- DispatcherServlet配置拦截所有请求 /
- 使用请求路径与所有加载的@RecuestMapping的内容进行比对
- 执行对应的方法
- 根据方法的返回值在webapp目录中查找对应的页面并展示
SpringMVC 技术架构图
- Dispatcherservlet:前端控制器,是整体流程控制的中心,由其调用其它组件处理用户的请求,有效的降低了组件间的耦合性
- HardlerMapping:处理器映射器,负责根据用户请求找到对应具体的Handler处理器
- Handler:处理器,业务处理的核心类,通常由开发者编写,描述具体的业务
- HardlAdaptez:处理器适配器,通过它对处理器进行执行
- View Resolver:视图解析器,将处理结果生成view视图
- view:视图,最终产出结果,常用视图如jsp、 html
基础配置
Controller加载控制
- SpringMVC的处理器对应的bean必须按照规范格式开发,为避免加入无效的bean可通过bean加载过滤器进行包含设定或排除设定,表现层bean标注通常设定为@Controller
在spring-mvc.xml
文件中修改成
<context:component-scan base-package="com.liu">
<context:include-filter
type="annotation"
expression="osg.springframework .stereotype.Controller" />
</context:component-scan>
或者在SpringMVCConfig.java
文件中修改成
package com.liu.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(
value = "com.liu",
includeFilters =
@ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = {
Controller.class}
)
)
public class SpringMVCConfig {
}
静态资源加载控制
- 核心控制器拦截的是所有请求,需要对静态资源请求进行放行,通过配置放行资源实现
<mvc:resources mapping="/img/**" 1ocation="/ing/" />
<mvc:resources mapping="/js/**" location="/js/" />
- 使用简化格式可以放行所有普通资源调用,无需一一枚举
<mve: default-servlet-handler />
在spring-mvc.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<context:component-scan base-package="com.liu" >
<!-- 为避免加入无效的bean可通过bean加载过滤器进行包含设定或排除设定 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 静态资源加载控制 -->
<!-- <mvc:resources mapping="/img/**" location="/img/" />-->
<!-- <mvc:resources mapping="/js/**" location="/js/" />-->
<!-- <mvc:resources mapping="/css/**" location="/css/" />-->
<!-- 简化:静态资源加载控制 -->
<mvc:default-servlet-handler />
</beans>
或者在SpringMVCConfig.java
文件中修改成
package com.liu.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@ComponentScan(
value = "com.liu",
includeFilters =
@ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = {
Controller.class}
)
)
public class SpringMVCConfig implements WebMvcConfigurer {
// @Override
// public void addResourceHandlers(ResourceHandlerRegistry registry) {
// // 静态资源加载控制
// registry.addResourceHandler("/img/**").addResourceLocations("/img/");
// registry.addResourceHandler("/js/**").addResourceLocations("/js/");
// registry.addResourceHandler("/css/**").addResourceLocations("/css/");
// }
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
// 简化:静态资源加载控制
configurer.enable();
}
}
中文乱码处理
- SpringMVC 提供专用的中文字符过滤器,用于处理乱码问题
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-olass>
<init-paran>
<paran-name>encoding</paran-nane>
<param-value>UTF-8</paran-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</fi1ter-name>
<uri-pattern>/*</ur1-pattern>
</fi1ter-mapping>
在 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">
<!-- 中文乱码处理 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class