一、Jsp九大内置对象和四大作用域
1)九大内置对象
名称 | 类型 | 含义 | 获取方式 |
request | HttpServletRequest | 封装所有请求信息 | 方法参数 |
response | HttpServletResponse | 封装所有响应信息 | 方法参数 |
session | HttpSession | 封装所有会话信息 | req.getSession() |
application | ServletContext | 项目所有信息 | getServletContext(); request.getServletContext() |
out | PrintWriter | 输出对象 | response.getWriter() |
exception | Exception | 异常对象 | |
page | Object | 当前页面对象 | |
pageContext | pageContext | 获取其他对象 | |
config | ServletConfig | 配置信息 |
2)四大作用域
1.page 在当前页面不会重新实例化
2.request 在一次请求中同一个对象,下次请求重新实例化一个request对象。
3.session 一次会话中只有一个session对象,依赖于cookie,只要Cookie中传递的Jsessionid不变,Session就不会重新实例化(在不超过默认生效时间的情况下)
实际的有效时间: A 浏览器关闭,Cookie失效
B默认时间 在时间范围内无任何交互。需在tomcat的web.xml中配置
<session-config>
<session-timeout>30</session-timeout>
</session-config>
4.application:只有在tomcat启动项目时实例化,只有当关闭tomcat服务器时销毁application
二.SpringMVC 作用域传值的几种方式
1)使用原生Servlet
在HandlerMethod参数中添加作用域对象
@RequestMapping("demo1")
public String demo1(HttpServletRequest abc,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-Diposition=“attachment;filename=文件名”
attachment 下载,以附件形式下载
filename 就是下载时显示的下载文件名
3)实现步骤
1.导入apatch的两个jar包
2.在 jsp 中添加超链接,设置要下载文件
在 springmvc 中放行静态资源 files 文件夹
<a href="download?fileName=a.rar">下载</a>
3.编写控制器方法
@RequestMapping("download")
public void download(String fileName,HttpServletResponse res,HttpServletRequest req)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)Spring中MultipartResovler组件的作用
1.把客户端上传的文件流转换成MutipartFile封装类
2.通过MutipartFile封装类获取到文件流。
3)表单数据类型分类
在<form>的 enctype 属性控制表单类型
1.默认值 application/x-www-form-urlencoded,为普通表单数据(少量文字信息)
2.text/plain 大文字量时使用的类型,例如邮件或论文
3.multipart/form-data 表单中包含二进制文件内容时用此属性值
4)实现步骤
1.导入springmvc 包和 apache 文件上传 commons-fileupload 和commons-io 两个 jar
2.下载功能所用的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>
3.配置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>
异常解析器可以在发生异常的时候,指定要跳转的页面
4.编写控制器类
MultipartFile 对象名必须和<input type=”file”/>的 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")) {
//唯一ID
String uuid = UUID.randomUUID().toString();
FileUtils.copyInputStreamToFile(file.getInputStream(),
new File("E:/" + uuid + suffix));
return "/index.jsp";
} else {
return "error.jsp";
}
}
形参的name为上传文件的文件名。