SpringMVC框架(二)

一、SpringMVC 作用域传值的几种方式

1、使用原生 Servlet

  • 在 HanlderMethod 参数中添加作用域对象
@RequestMapping("demo1")
public String demo1(HttpServletRequestabc,HttpSession sessionParam){
	//request 作用域
	abc.setAttribute("req", "req 的值");
	//session 作用域
	HttpSession session = abc.getSession();
	session.setAttribute("session", "session 的值");
	sessionParam.setAttribute("sessionParam",
	"sessionParam 的值");
	//appliaction 作用域
	ServletContext application = abc.getServletContext();
	application.setAttribute("application","application 的值");
	return "/index.jsp";
}

2、使用 Map 集合

  • 把 map 中内容放在 request 作用域中
  • spring 会对 map 集合通过 BindingAwareModelMap 进行实例化
@RequestMapping("demo2")
public String demo2(Map<String,Object> map){
	System.out.println(map.getClass());
	map.put("map","map 的值");
	return "/index.jsp";
}

3、使用 SpringMVC 中 Model 接口

  • 把内容最终放入到 request 作用域中.
@RequestMapping("demo3")
public String demo3(Model model){
	model.addAttribute("model", "model 的值");
	return "/index.jsp";
}

4、使用 SpringMVC 中 ModelAndView 类

@RequestMapping("demo4")
public ModelAndView demo4(){
//参数,跳转视图
	ModelAndView mav = new ModelAndView("/index.jsp");
	mav.addObject("mav", "mav 的值");
	return mav;
}

二、文件下载

1.访问资源时相应头如果没有设置 Content-Disposition,浏览器默认按照 inline 值进行处理

  • inline 能显示就显示,不能显示就下载.

2.只需要修改相应头中 Context-Disposition=”attachment;filename=文件名”

  • attachment 下载,以附件形式下载.
  • filename=值就是下载时显示的下载文件名

3.实现步骤
3.1、导入 apatch 的两个 jar
在这里插入图片描述
3.2、在 jsp 中添加超链接,设置要下载文件

  • 在 springmvc 中放行静态资源 files 文件夹
<a href="download?fileName=a.rar">下载</a>

3.3、编写控制器方法

@RequestMapping("download")
public void download(StringfileName,HttpServletResponse res,HttpServletRequestreq) throws IOException{
	//设置响应流中文件进行下载
	res.setHeader("Content-Disposition","attachment;filename="+fileName);
	//把二进制流放入到响应体中.
	ServletOutputStream os = res.getOutputStream();
	String path = req.getServletContext().getRealPath("files");
	System.out.println(path);
	File file = new File(path, fileName);
	byte[] bytes = FileUtils.readFileToByteArray(file);
	os.write(bytes);
	os.flush();
	os.close();
}

三、文件上传

1、基于 apache 的 commons-fileupload.jar 完成文件上传.
2、MultipartResovler 作用:

  • 把客户端上传的文件流转换成 MutipartFile 封装类.
  • 通过 MutipartFile 封装类获取到文件流

3、表单数据类型分类

  • 在的 enctype 属性控制表单类型
  • 默认值 application/x-www-form-urlencoded,普通表单数据.(少量文字信息)
  • text/plain 大文字量时使用的类型.邮件,论文
  • multipart/form-data 表单中包含二进制文件内容.

4、实现步骤:

  • 导入 springmvc 包和 apache 文件上传 commons-fileupload 和commons-io 两个 jar
  • 编写 JSP 页面
<form action="upload" enctype="multipart/form-data" method="post">
	姓名:<input type="text" name="name"/><br/>
	文件:<input type="file" name="file"/><br/>
	<input type="submit" value=" 提交 "/>
</form>
  • 配置 springmvc.xml
<!-- MultipartResovler 解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	<property name="maxUploadSize" value="50"></property>
</bean>
<!-- 异常解析器 -->
<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
	<props>
		<prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">/error.jsp</prop>
	</props>
</property>
</bean>
  • 编写控制器类 MultipartFile 对象名必须和的 name 属性值相同
@RequestMapping("upload")
public String upload(MultipartFile file,String name) throws IOException{
	String fileName = file.getOriginalFilename();
	String suffix = fileName.substring(fileName.lastIndexOf("."));
	//判断上传文件类型
	if(suffix.equalsIgnoreCase(".png")){
		String uuid = UUID.randomUUID().toString();
		FileUtils.copyInputStreamToFile(file.getInputStream(), new File("E:/"+uuid+suffix));
		return "/index.jsp";
	}else{
		return "/error.jsp";
	}
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值