Spring MVC 从入门到精通

1 简介

1.1 重要组件

1.1.1 DispatcherServlet

前端控制器,接受所有请求(如果配置/,不包含jsp)

1.1.2HandlerMapping

解析请求格式,判断希望要执行哪个具体的方法

1.1.3 HandlerAdapter

负责调用具体的方法

1.1.4 ViewResovler

视图解析器
解析结果,准备跳转到具体的物理视图

1.2 运行原理

在这里插入图片描述

2 环境搭建

1、导入jar包
2、在web.xml中配置前端控制器DispatcherServlet

<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>	
	</servlet>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>	

如果不配置init-name,回去web-inf中寻找-servlet.xml
3、在src下新建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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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/aop
	https://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/context
	https://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/tx
	https://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/mvc
	https://www.springframework.org/schema/mvc/spring-mvc.xsd">
	<!-- 扫描注解 -->
	<context:component-scan base-package="com.zy.controller"></context:component-scan>
	<!-- 注解驱动 -->
	<!-- 包含了handlermapping   handleradapter -->
	<mvc:annotation-driven></mvc:annotation-driven>
	<!-- 静态资源 -->
	<mvc:resources location="/js" mapping="/js/**"></mvc:resources>
</beans>

必须在springmvc中扫描controller,装载到springmvc的容器中
4、编写控制器类

package com.zy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class DemoController {
	@RequestMapping("demo")
	public String demo() {
		return "main.jsp";
	}
	@RequestMapping("demo2")
	public String demo2() {
		return "main1.jsp";
	}
}

3 字符编码过滤器

<filter>
		<filter-name>encoding</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>encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

4 传参

1、把内容写到方法(HandlerMethord)参数中,SpringMVC只要有这个内容,注入内容

	@RequestMapping("demo2")
	public String demo2(People peo) {
		System.out.println(peo);
		return "main1.jsp";

2、jsp中直接可以传参

<body>
	<form action="demo2" method="post">
		姓名:<input type="text" name="name"/>
		</br>
		年龄:<input type="text" name="age"/>
		</br>
		<input type="submit" value="提交">
	</form>
</body>

3、参数注解配置
在这里插入图片描述
最后一个必须传递参数,如果不传,为null,会报错
required与defaultValue不要一起用

4.2 复杂参数传递

4.2.1 请求参数包含多个同名参数

复选框传递参数
checkbox

<form action="demo2" method="post">
		姓名:<input type="text" name="name"/>
		</br>
		年龄:<input type="text" name="age"/>
		</br>
		<input type="checkbox" name="hover" value="写代码">
		<input type="checkbox" name="hover" value="刷B站">
		<input type="checkbox" name="hover" value="剪辑视频">  
		<input type="submit" value="提交">
	</form>
@RequestMapping("demo2")
	public String demo2(People peo,@RequestParam("hover") List list) {
		System.out.println(peo+" "+list);
		return "main1.jsp";
	}

4.2.2 请求对象中对象.属性格式

jsp
在这里插入图片描述
新建一个类
对象名和参数点前面的名称相对应
在这里插入图片描述
控制器
在这里插入图片描述

4.2.3 在请求参数中传递集合类型参数

在这里插入图片描述
同上

4.2.4 restful传值方式

简化jsp中参数编写格式
在jsp中设定特定的格式

<a href="demo3/21/赵研">跳转</a>

在控制器中
在@RequestMapping中,和请求格式对应
{名称}是自定义名称
@PathVariable获取@RequestMapping中内容
/main.jsp是默认从跟目录下寻找

@RequestMapping("demo3/{age}/{name}")
	public String demo3(@PathVariable String name,@PathVariable("age") String age1) {
		System.out.println(name+" "+age1);
		return "/main.jsp";
	}

5 视图解析器

5.1 跳转方式

默认的跳转方式为请求转发
设定返回值字符串内容
添加 redirect:资源路径 重定向
添加 forward:资源路径 转发

5.2 自定义视图解析器

Spring会默认配置视图解析器
程序自定义视图解析器

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

如果不希望使用自定义的视图解析器,在方法前面添加redirect或者forward

@RequestMapping("demo4")
	public String demo4() {
		return "forward:demo5";
	}
	@RequestMapping("demo5")
	public String demo5()
	{
		System.out.println("demo5");
		return "main";
	}

5.3 @ResponseBody

在方法上只有@RequestMapping,无论方法是什么,认为都只会跳转

@RequestMapping("demo6")
	@ResponseBody
	public People demo6() {
		People peo=new People();
		peo.setAge(21);
		peo.setName("赵研");
		return peo;
	}

1、如果返回值满足key-value形式(对象、map),RequestMapping会返回josn
application/josn
把转换后的内容以输出流的形式响应给客户端
恒不跳转
2、如果返回值不满足key-value形式,String
把相应头设置为text/html/charset=utf-8
把返回值以流的形式输出
如果包含中文
在@RequestMapping中加入produce
表示content-type的取值
在这里插入图片描述
在这里插入图片描述

6 JSP九大内置对象和四大作用域

6.1 九大内置对象

在这里插入图片描述
在这里插入图片描述

6.2 四大作用域

page
request
session
application
在这里插入图片描述
在这里插入图片描述

6.3 作用域传值

1、使用原生servlet API
在HandlerMethord 中添加
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

7 文件下载

访问资源时响应头如果没有设置

@RequestMapping("downLoad")
	public void downLoad(String fileName,HttpServletResponse res,HttpServletRequest req) throws IOException {
		//将二进制流放到响应体中
		ServletOutputStream os = res.getOutputStream();
		//设置响应流中文件进行下载
		res.setHeader("Content-Disposition", "attachment;fileName=test.txt");
		String path = req.getServletContext().getRealPath("files");
		System.out.println(path);
		File file = new File(path,fileName);
		//把文件变为字节数组
		byte[] bin = FileUtils.readFileToByteArray(file);
		os.write(bin);
		os.flush();
		os.close();
	}
<body>
	<a href="downLoad?fileName=a.txt">下载</a>
</body>

7 文件上传

基于aphce的Commons-fileupload完成文件上传的
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值