兴化网站制作,贵阳白云网站建设,网页微信版官网登录下载,在哪学短视频培训课程springboot傻瓜式教程用久了#xff0c;回过来研究下SSM的工作流程#xff0c;当然从Spring MVC开始#xff0c;从傻瓜式入门处理请求和页面交互#xff0c;再到后面深入源码分析。 本人写了一年多的后端和半年多的前端了。用的都是springbioot和vue#xff0c;源码一直来… springboot傻瓜式教程用久了回过来研究下SSM的工作流程当然从Spring MVC开始从傻瓜式入门处理请求和页面交互再到后面深入源码分析。 本人写了一年多的后端和半年多的前端了。用的都是springbioot和vue源码一直来不及时间看就先从SSM开刀吧 一、SpringMvc入门之用tomcat插件使用MVCweb项目打印Hello World
1、新建一个空项目
File- New-Project -----选择Maven 然后SDK选择1.8
2、新建一个模块开始写web项目
File- New - Module 下一步 - 创建项目的名称 然后填写自己的maven坐标即可完成模块的创建。
3、填写maven依赖
1、servlet-api的依赖 这个一定要写 scopeprovided/scope 不然tomcat启动失败 2、既然mvc就要添加webmvc依赖 3、填写tomcat插件依赖 最后 记得刷新maven
?xml version1.0 encodingUTF-8?project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.example/groupIdartifactIdSpringMvcSimple/artifactIdversion1.0-SNAPSHOT/versionpackagingwar/packagingnameSpringMvcSimple Maven Webapp/name!-- FIXME change it to the projects website --urlhttp://www.example.com/urlpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingmaven.compiler.source1.7/maven.compiler.sourcemaven.compiler.target1.7/maven.compiler.target/propertiesdependenciesdependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.11/versionscopetest/scope/dependencydependencygroupIdjavax.servlet/groupIdartifactIdjavax.servlet-api/artifactIdversion3.1.0/version!-- 这里确实必须设置 不然tomcat启动会失败 --scopeprovided/scope/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion4.3.10.RELEASE/version/dependency/dependenciesbuildfinalNameSpringMvcSimple/finalNamepluginsplugingroupIdorg.apache.tomcat.maven/groupIdartifactIdtomcat7-maven-plugin/artifactIdversion2.1/versionconfigurationport8088/portpath//path/configuration/plugin/plugins/build
/project
4、配置tomcat容器 1、记得一定要配置工作目录 选择你要启动的项目目录 2、命令行 写 tomcat7:run 这个是会自己提示的如果没有提示说明你的tomcat插件依赖有问题记得刷新maven依赖
3、另外这个tomcat启动其实也可以直接从你项目的依赖下启动 即双击即可启动容器
4、项目启动
1、写一个Controller 用来处理请求
Controller
public class HelloController {RequestMapping(/hello)ResponseBodypublic String hello(){return hello world123;}
}2、配置web mvc环境
这个主要用来配置mvc环境以及 将我们需要的配置注册进来。SpringMvcConfig被注册进来
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {/*** 用于创建并配置DispatcherServlet应用程序上下文。* return*/Overrideprotected WebApplicationContext createServletApplicationContext() {AnnotationConfigWebApplicationContext ctx new AnnotationConfigWebApplicationContext();ctx.register(SpringMvcConfig.class);return ctx;}/*** 是一个抽象方法返回DispatcherServlet映射路径的字符串数组。* 哪些路径被mvc 管理* return*/Overrideprotected String[] getServletMappings() {return new String[]{/};//所有路径交给 mvc 管理}Overrideprotected WebApplicationContext createRootApplicationContext() {return null;}
}mvc配置 此时自动去扫描我们的com.item.controller包注入到容器中
Configuration
ComponentScan({com.item.controller})
public class SpringMvcConfig {
}接下来启动tomcat 访问 端口 http://localhost:8088/hello 即可将请求打到控制层 返回数据 hello world123 至此一个简单的mvc web应用就启动了。
5、优化加载配置
我们上面用的继承 AbstractDispatcherServletInitializer这个类然后加载我们的mvc配置进而初始化webmvc容器接下来用 继承 AbstractAnnotationConfigDispatcherServletInitializer完成同样的工作。
public class ServletContainersInitConfig2 extends AbstractAnnotationConfigDispatcherServletInitializer {Overrideprotected Class?[] getRootConfigClasses() {return new Class[0];}Overrideprotected Class?[] getServletConfigClasses() {return new Class[]{SpringMvcConfig.class};}Overrideprotected String[] getServletMappings() {return new String[]{/};}
}这个也能完成初始化webmvc的配置
6、处理乱码问题
post请求乱码
post请求处理乱码注意是post请求 添加一个中文编码的过滤器即可 在 刚才写的 AbstractAnnotationConfigDispatcherServletInitializer继承此类 实现其 getServletFilters 即可
Overrideprotected Filter[] getServletFilters() {CharacterEncodingFilter filternew CharacterEncodingFilter();filter.setEncoding(Charset.defaultCharset().name());return new Filter[]{filter};}然后使用 curl命令发送post请求
curl http://localhost:8088/chinese -d name你好 -d address哈哈控制台打印未出现乱码.
get请求乱码
这个是发送请求是 参数不在请求体中直接在url后面。这个可以在tomcat插件配置中增加 uriEncodingUTF-8/uriEncoding解决。 发送get请求 curl http://localhost:8088/chinese?name你好address123 发现控制台就不乱码了
7、页面交互处理静态资源
1、报错过程分析
在webapp目录下创建 views/books.html
!DOCTYPE html
html langen
headmeta charsetUTF-8titleTitle/title
/head
body
h1欢迎来到书籍页面/h1
/body
/html
接下来浏览器输入 http://localhost:8088/views/books.html 报错
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/7.0.37很显然404找不到资源了。说明mvc不知道如何处理这个资源。它实际上是把这个views/books.html当成请求去controler层匹配请求了。但是我们这个是静态页面实际就是去访问静态资源而已。那么接下来就是这么让容器知道这个是静态资源不把它当成请求处理呢
2、springMVC放行静态资源交给tomcat容器处理
造成的原因 显然在 ServletContainersInitConfig2 类中 我们springMvc拦截了所有的请求。 Overrideprotected String[] getServletMappings() {return new String[]{/};}解决办法有很多种此处是 继承 WebMvcConfigurationSupport类 然后实现其 添加资源方法 进行放行静态资源。同时记得 此类需要加载到 mvc环境中具体看下面步骤
1、配置类过滤静态资源
在 com.item.config 包下 编写 SpringMvcSupport 去实现过滤静态资源 一、注意加上Configuration 这个是为了 SpringMvcConfig 去扫描这个包时把它注入到mvc环境中 二、重写 addResourceHandlers 方法 这个就是处理映射请求的。
Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {/*** 资源处理* param registry*/Overrideprotected void addResourceHandlers(ResourceHandlerRegistry registry) {registry//当访问 /views/** 这个路径 我们让它 访问资源路径 /views/.addResourceHandler(/views/**).addResourceLocations(/views/);super.addResourceHandlers(registry);}
}2、注入到Mvc环境中
一、我们新加了 com.item.config 这个包 就是让他去扫描这个包 注入Mvc中的
Configuration
ComponentScan({com.item.controller,com.item.config})
public class SpringMvcConfig {
}最后
启动tomcat容器访问 http://localhost:8088/views/books.html 可以看到页面
欢迎来到书籍页面
即成功。