SpringMVC——学习流程
一个模型 - 视图 - 控制器(MVC)的Web框架建立在中央前端控制器
spring官方文档:
https://spring.io/guides/gs/serving-web-content/
1、简单的hello.jsp请求响应
流程:
- 导jar包
- 在web.xml加入spring监听,以及springmvc的核心配置(配置文件的地址,拦截器)
- 配置applicationContext.xml(扫描除Controller类;导入pringmvc.xml)和pringmvc.xml(扫描Controller类;配置视图解析器)
- Controller类编写
- hello.jsp编写
- 导jar包 :(pom.xml)
<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>
<spring.version>5.1.9.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>compile</scope>
</dependency>
<!--springmvc jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
- spring监听,以及springmvc的核心配置(web.xml)
<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_3_1.xsd"
version="3.1"
metadata-complete="false">
<display-name>Archetype Created Web Application</display-name>
<!--spring监听器 在项目启动时加载配置文件,实例化Bean-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--spring配置文件 contextConfigLocation参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/applicationContext.xml</param-value>
</context-param>
<!--前端控制器 springmvc配置文件 contextConfigLocation参数-->
<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*:config/springmvc.xml</param-value>
</init-param>
</servlet>
<!--拦截器-->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
- 配置applicationContext.xml和springmvc.xml
- 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描Controller类的包,依赖注解注入(annotation),关闭扫描包下的所有内容(use-default-filters)-->
<context:component-scan base-package="com.qst.springmvc.controller" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- 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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描包 排除Controller类-->
<context:component-scan base-package="com.qst.springmvc">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--导入springmvc.xml-->
<import resource="classpath*:config/springmvc.xml"/>
</beans>
- Controller类编写
@Controller
public class HelloController {
@RequestMapping("hello")
public ModelAndView hello(){
return new ModelAndView("hello");
}
}
- hello.jsp编写
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>hello</title>
</head>
<body>
<h2>Hello!</h2>
</body>
</html>
- 运行Tomcat(配置一下)
- 运行结果(http://localhost:8080/springmvc02/hello)
- 项目架构
2、了解springMVC的执行流程
http://localhost:8080/springmvc02/hello
(1)DispatcherServlet拦截请求
(2)根据请求调用HandlerMapper(处理器映射器)查找路径:springmvc02/hello——Controller上的RequestMapping标注的地址
(3)查找的hello返回给DispatcherServlet
(4)DispatcherServlet调用HandlerAdapter(处理器适配器)
(5)HandlerAdapter调用并执行Handler(处理器),即所编写的Controller类
(6)Controller执行完,返回一个ModelAndView对象,将对象返回给DispatcherServlet
(7)DispatcherServlet调用ViewReslover(视图解析),即InternalResourceViewResolver——/view/hello.jsp
(8)查找到的具体的View视图返回给DispatcherServlet
(9)DispatcherServlet对View进行渲染:Handler——静态页面,浏览器显示渲染结果
3、注解使用——请求处理RequestMapping
属性 | 用途 | 使用 |
---|---|---|
value | 指定地址,是默认值,可以映射多个路径 | @RequestMapping(“hello”); @RequestMapping("/hello"); @RequestMapping(value = “hello”); @RequestMapping(value = {“hello”, “hello01”} |
path | 地址,相当于划分空间package | @RequestMapping(path = “hello”, name = “RequestMapping模块”) |
method | 指定请求method的类型 | method = {RequestMethod.POST,RequestMethod.GET} |
consumes | 指定处理请求的提交内容类型 | @RequestMapping(value = “consumes”,consumes = “multipart/form-data”) |
Content-type | 具体请求中的媒体类型信息 | application/x-www-form-urlencoded(表单提交的标准编码格式,默认格式);multipart/form-data(用于文件上传);application/json(将数据以json的对象格式传递);text/html(数据以纯文本形式进行编码) |
params | 指定客户端传递参数的值 | @RequestMapping(value = “params”,params = {“username=king”,“password=123”}) |
headers | 指定客户端请求头必须包含的属性值 | @RequestMapping(value = “headers”,headers = “Cookie=JSESSIONID=C42C68C75A931A63A252BF71E74D59F8”) |
produces | 服务器响应客户端的数据类型 | @RequestMapping(value = “produces”,produces = “application/xml;charset=UTF-8”);@RequestMapping(value = “produces2”,produces = “application/json;charset=UTF-8”) |
- 使用json,需要配置静态资源(springmvc.xml),否则页面不会响应
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />
<!--对静态资源文件的访问-->
<mvc:resources mapping="/static/**" location="/static/" />
jquery官方文档:
https://jquery.com/download/
jquery帮助文档:
- 页面请求头内容:
- value
@Controller
@RequestMapping(path = "hello", name = "hello模块")
public class HelloController {
//@RequestMapping("hello")
//@RequestMapping("/hello")
//@RequestMapping(value = "hello")
//@RequestMapping(value = {"hello", "hello01"})
@RequestMapping(value = "/hello")
public ModelAndView hello() {
return new ModelAndView("hello");
}
}
- 其他属性
/**
* 请求映射
*/
@Controller
@RequestMapping(path = "mvc2", name = "RequestMapping模块")
public class HelloController2 {
//通过浏览器地址栏修改地址,都是get请求,请求参数地址栏限制,长度1K
//一般在页面进行请求
//路径会变成mvc2/selectMethod mvc2/findMethod
@RequestMapping(value = {"selectMethod","findMethod"},method = RequestMethod.POST)
public ModelAndView selectMethod() {
return new ModelAndView("hello");
}
//请求action中要写上参数值,或者input里
@RequestMapping(value = "params",params = {"username=king","password=123"})
public ModelAndView params() {
return new ModelAndView("hello");
}
//请求头满足条件可跳转
/*@RequestMapping(value = "headers",headers = "Content-Type=application/x-www-form-urlencoded")
public ModelAndView headers() {
return new ModelAndView("hello");
}*/
@RequestMapping(value = "headers",headers = "Cookie=JSESSIONID=C42C68C75A931A63A252BF71E74D59F8")
public ModelAndView headers() {
return new ModelAndView("hello");
}
//服务端可以接收的媒体类型
@RequestMapping(value = "consumes",consumes = "multipart/form-data")
public ModelAndView consumes() {
return new ModelAndView("hello");
}
/*服务器响应客户端的类型*/
@RequestMapping(value = "produces",produces = "text/html;charset=UTF-8")
@ResponseBody
public String produces() {
return "<h1 style='color:red'>你好 你好</h1>";
}
/*服务器响应客户端的类型*/
//使用json,先配置一下。ajax请求不会跳转页面。
@RequestMapping(value = "produces2",produces = "application/json;charset=UTF-8")
@ResponseBody //处理数据,返回数据不会使用视图器解析器
public String produces2() {
System.out.println("--------");
return "<h1 style='color:red'>你好 你好</h1>";
}
- 页面index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<base href="${pageContext.request.contextPath}/">
<title>hello</title>
<%--有些浏览器不加载下划线结尾的--%>
<script src="static/js/jquery.min.js"></script>
<script language="JavaScript">
var user ={"username":"king"};
$(function(){
//alert();
$("#ajaxBtn").click(f