流程
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200726163733436.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcxMTk5Mg==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200726164024585.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcxMTk5Mg==,size_16,color_FFFFFF,t_70)
web.xml中配置的DispatcherServlet
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200726170316841.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcxMTk5Mg==,size_16,color_FFFFFF,t_70)
配置spingmvc三大要素:处理器映射器、处理器适配器、视图解析器
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200726171020238.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcxMTk5Mg==,size_16,color_FFFFFF,t_70)
注册控制器,id为访问地址
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200726171921997.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200726175200868.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcxMTk5Mg==,size_16,color_FFFFFF,t_70)
注解配置springxml文件
HelloSpringmvc注解配置代码
spring 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:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.kuang.controller"/><!--扫描这个包下,所有注解生效-->
<mvc:annotation-driven/><!--配置注解驱动-->
<mvc:default-servlet-handler/><!--静态资源过滤-->
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
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">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
测试代码
package com.kuang.controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@org.springframework.stereotype.Controller
public class Controller {
@RequestMapping("/h1")
public String msg(Model model){
model.addAttribute("msg","HelloSpringmvcAnnotation");
return "test";
}
}
注意
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200726192114611.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcxMTk5Mg==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200726194650749.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcxMTk5Mg==,size_16,color_FFFFFF,t_70)
遇到的问题:代码没问题的前提下提示404页面
原因:tomcat本地项目目录中maven依赖在发布项目的过程中没有及时导入依赖
解决办法:
1、IDEA中打开项目的project structure
2、找到artifact功能,并选中发布的项目
3、在WEB-INF目录下,(与classes文件夹处于同一目录)新建一个lib目录
4、点击上方+号,添加libiray,选中所有依赖添加
5、apply—>ok
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200726195603544.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcxMTk5Mg==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200726200350135.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcxMTk5Mg==,size_16,color_FFFFFF,t_70)
Restful风格
**==============================================**
Json
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200727201228571.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcxMTk5Mg==,size_16,color_FFFFFF,t_70)
标注了@RestController类下的所有方法只会返回json字符串
而@Controller会走视图解析器,返回的字符串会用来拼接域名
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200727203039315.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200727204838775.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcxMTk5Mg==,size_16,color_FFFFFF,t_70)
遇到的问题:properties文件中属性名前必须加jdbc.,不加就会报错
原因
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200728221757193.png)
ajax技术
状态码
200:成功
300+:转发/重定向
400+:客户端错误
500+:服务器端错误
![在这里插入图片描述](https://img-blog.csdnimg.cn/2020073015075480.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcxMTk5Mg==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200730151250550.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcxMTk5Mg==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200730152107119.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTcxMTk5Mg==,size_16,color_FFFFFF,t_70)
文件上传下载