一、日期赋值

目标:在springMVC中日期赋值兼容性更广泛

不能直接处理,必须使用转换器
1、定义转换器,实现接口Converter<From,To>

  1. package com.zy.converter;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import org.springframework.core.convert.converter.Converter;
  6. //从字符串转日期
  7. public class MyDateConverter implements Converter<String, Date> {
  8. @Override
  9. public Date convert(String value) {//参数就是传入的字符串日期
  10.       //1999年6月6日
  11. //1999-6-6
  12. //1999.6.6
  13. //1999/6/6 默认支持
  14. // 第一种为例
  15. //1创建相对应的日期格式化对象
  16. SimpleDateFormat simpleDateFormat = null;
  17. try {
  18. if(value.contains("年")){
  19. simpleDateFormat= new SimpleDateFormat("yyyy年MM月dd日");
  20. }else if(value.contains("-")){
  21. simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd");
  22. }else if(value.contains(".")){
  23. simpleDateFormat= new SimpleDateFormat("yyyy.MM.dd");
  24. }else if(value.contains("/")){
  25. simpleDateFormat= new SimpleDateFormat("yyyy/MM/dd");
  26. }
  27.  
  28. //2把字符串解析成一个日期对象
  29. Date parse = simpleDateFormat.parse(value);
  30. //3返回结果
  31. return parse;
  32. } catch (ParseException e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. }
  36. return null;
  37. }
  38.  
  39. }

2、配置

spring.xml中

  1. <!-- 2配置日期转换器 -->
  2. <bean id="formattingConversion" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
  3. <property name="converters">
  4. <list>
  5. <!-- 配置自己的转换器 一个bean就是一个类-->
  6. <bean class="com.zy.converter.MyDateConverter"></bean>
  7. </list>
  8. </property>
  9. </bean>
  10. <!-- 3引用上文的转换器 -->
  11. <mvc:annotation-driven conversion-service="formattingConversion"></mvc:annotation-driven>

3、Upload上传

1)导包

2)多功能表单

  1. <form action="file/up" method="post" enctype="multipart/form-data"><!-- 多功能表单 -->
  2. 头像:<input type="file" name="myfile"/><input type="submit"/>
  3. </form>

3)文件上传解析器

spring.xml

  1. <!-- 4文件上传解析器 id名为multipartResolver,不然可能会报错-->
  2. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  3. <property name="maxUploadSize">
  4. <value>5242880</value>
  5. </property>
  6. </bean>

4)辅助方法+上传文件

  1. @Controller
  2. @RequestMapping("/file")
  3. public class FileController {
  4. //辅助方法
  5. //1根据逻辑路径得到真实路径
  6.  
  7. //过期的
  8. //@SuppressWarnings(“deprecation”)表示不检测过期的方法
  9. @SuppressWarnings("deprecation")
  10. public String myGetRealPath(String path,HttpServletRequest request){
  11. String realPath = request.getRealPath(path);
  12. System.out.println("真实路径:"+realPath);
  13. File file = new File(realPath);
  14. if(!file.exists()){
  15. file.mkdirs();
  16. }
  17.  
  18. return realPath;
  19. }
  20.  
  21. //2更改文件名
  22. public String newFileName(MultipartFile file){
  23. String originalFilename = file.getOriginalFilename();
  24. //abc.jpg
  25. //截取后缀,拼接新的文件名
  26. //后缀
  27. String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
  28. //新文件名要求:上传中防止文件名重复,发生覆盖
  29. String uu = UUID.randomUUID().toString();
  30.  
  31. String newName=uu+substring;
  32. return newName;
  33.  
  34. }
  35.  
  36. // @Test
  37. // public void test(){
  38. // System.out.println(UUID.randomUUID());
  39. // }
  40.  
  41. //上传--//如果controller只需要跳转页面的话,可以把返回值写成String 不用写成ModelAndView
  42. @SuppressWarnings("resource")
  43. @RequestMapping("/up")
  44. public String up(MultipartFile myfile,HttpServletRequest request) throws Exception{
  45. //得到真实路径 <!--tomcat服务器来给该request赋值-->
  46. String path="/img";//逻辑路径
  47. String myGetRealPath = myGetRealPath(path, request);
  48. //得到新的文件名
  49. String newFileName = newFileName(myfile);
  50.  
  51. //上传----把本地文件按流的方式copy到服务器上
  52.  
  53. //输入流
  54. InputStream is = myfile.getInputStream();
  55. //输出流
  56. FileOutputStream os = new FileOutputStream(myGetRealPath+"/"+newFileName);
  57. //copy
  58. IOUtils.copy(is, os);
  59. request.setAttribute("img",path+"/"+newFileName);
  60. os.close();
  61. is.close();
  62. return "/index.jsp";
  63. }

4、下载

  1. //图片下载
  2. @SuppressWarnings("resource")
  3. @RequestMapping("/down")
  4. public void down(HttpServletResponse response,String fileName,HttpServletRequest request) throws Exception {
  5. //设置头--[下载attachment/预览]
  6. response.setHeader("Content-Disposition", "attachment;fileName="+fileName);
  7. //下载的本质--文件按流的方式从服务器copy到本地
  8. //得到资源在服务器的真实路径
  9. String path="/aaa/"+fileName;
  10. String myGetRealPath = myGetRealPath(path, request);
  11. FileInputStream fileInputStream = new FileInputStream(myGetRealPath);
  12. ServletOutputStream outputStream = response.getOutputStream();
  13.  
  14. IOUtils.copy(fileInputStream, outputStream);
  15. fileInputStream.close();
  16. outputStream.close();
  17. //下载以后不要跳页面
  18. }

5、ModeAndView

  1. @RequestMapping("/go")
  2. public ModelAndView go(){
  3. ModelAndView modelAndView = new ModelAndView();
  4. //modelAndView分为两个功能-----我们以前见过的
  5. //model 数据
  6. //view 视图
  7. //------
  8. //存域
  9. modelAndView.addObject("mydata","880");
  10. //跳转
  11. modelAndView.setViewName("/abc.jsp");
  12. return modelAndView;
  13.  
  14. }

6、视图解析器(便利性)

spring.xml

  1. <!-- 5视图解析器 -->
  2. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

      <!-- 前缀 -->
  3. <property name="prefix" value="/WEB-INF/view/"/>
  4. <!-- 后缀 -->
    <property name="suffix" value=".jsp"/>
  5. </bean>
  1. @RequestMapping("/show")
  2. public String show(){
  3. //带视图解析器的跳转
  4. // /WEB-INF/view/
  5. // .jsp
  6. return "aaa";
  7. }
  8.  
  9. @RequestMapping("/mmm")
  10. public String mmm(){
  11.  
  12. return "forward:/WEB-INF/mmm/bbb.jsp";//指定响应方式可以摆脱视图解析器
  13. }
  14.  
  15. //----
  16. ///WEB-INF下的页面不能通过重定向到
  17. //return "forward:/WEB-INF/mmm/bbb.jsp" 转发
  18. //return "redirect:bbb.jsp" 重定向
  1. <hr>
  2. 文件上传
  3. <form action="${pageContext.request.contextPath}/file/up.action" method="post" enctype="multipart/form-data"><!-- 多功能表单 -->
  4. 头像:<input type="file" name="myfile"/><input type="submit"/>
  5. </form>
  6. <img alt="" src="${pageContext.request.contextPath}${img}">
  7. </body>
  8. 文件下载
  9. <a href="${pageContext.request.contextPath}/file/down.action?fileName=110.jpg">下载</a>
  10.  
  11. <a href="${pageContext.request.contextPath}/file/go.action?">跳转</a>
  12.  
  13. <a href="${pageContext.request.contextPath}/file/show.action?">带视图解析器跳转</a>
  14.  
  15. <a href="${pageContext.request.contextPath}/file/mmm.action?">带视图解析器跳转2</a>

6、url-pattern

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  3. <display-name>springmvc01</display-name>
  4. <welcome-file-list>
  5. <welcome-file>index.html</welcome-file>
  6. <welcome-file>index.htm</welcome-file>
  7. <welcome-file>index.jsp</welcome-file>
  8. <welcome-file>default.html</welcome-file>
  9. <welcome-file>default.htm</welcome-file>
  10. <welcome-file>default.jsp</welcome-file>
  11. </welcome-file-list>
  12.  
  13. <!-- 配置前端控制器 -->
  14. <servlet>
  15. <servlet-name>aaa</servlet-name>
  16. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  17. <!-- 加载配置文件 -->
  18. <init-param>
  19. <param-name>contextConfigLocation</param-name>
  20. <param-value>classpath:spring.xml</param-value><!-- 配置文件的位置 classpath代表src -->
  21. </init-param>
  22. <load-on-startup>1</load-on-startup>
  23. </servlet>
  24.  
  25. <servlet-mapping>
  26. <servlet-name>aaa</servlet-name>
  27. <url-pattern>*.action</url-pattern><!-- 拦截规则 --><!-- 后缀拦截 拦截以action结尾的请求-->
  28. </servlet-mapping>
  29.  
  30. <!-- / /* /*范围更广,包括jsp的拦截 -->
  31.  
  32. <!-- 配置编码过滤器 -->
  33. <filter>
  34. <filter-name>characterEncodingFilter</filter-name>
  35. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  36. <init-param>
  37. <param-name>encoding</param-name>
  38. <param-value>UTF-8</param-value>
  39. </init-param>
  40. </filter>
  41. <filter-mapping>
  42. <filter-name>characterEncodingFilter</filter-name>
  43. <url-pattern>/*</url-pattern>
  44. </filter-mapping>
  45.  
  46. </web-app>

SpringMVC学习笔记2的更多相关文章

  1. 史上最全的SpringMVC学习笔记

    SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...

  2. springmvc学习笔记--REST API的异常处理

    前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...

  3. springmvc学习笔记---面向移动端支持REST API

    前言: springmvc对注解的支持非常灵活和飘逸, 也得web编程少了以往很大一坨配置项. 另一方面移动互联网的到来, 使得REST API变得流行, 甚至成为主流. 因此我们来关注下spring ...

  4. SpringMVC:学习笔记(8)——文件上传

    SpringMVC--文件上传 说明: 文件上传的途径 文件上传主要有两种方式: 1.使用Apache Commons FileUpload元件. 2.利用Servlet3.0及其更高版本的内置支持. ...

  5. springmvc学习笔记(简介及使用)

    springmvc学习笔记(简介及使用) 工作之余, 回顾了一下springmvc的相关内容, 这次也为后面复习什么的做个标记, 也希望能与大家交流学习, 通过回帖留言等方式表达自己的观点或学习心得. ...

  6. springmvc学习笔记(常用注解)

    springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...

  7. SpringMVC学习笔记之二(SpringMVC高级参数绑定)

    一.高级参数绑定 1.1 绑定数组 需求:在商品列表页面选中多个商品,然后删除. 需求分析:功能要求商品列表页面中的每个商品前有一个checkbok,选中多个商品后点击删除按钮把商品id传递给Cont ...

  8. springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定

    springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定 标签: springmvc springmvc学习笔记13-springmvc注解开发之集合类型參数绑定 数组绑定 需 ...

  9. springmvc学习笔记(19)-RESTful支持

    springmvc学习笔记(19)-RESTful支持 标签: springmvc springmvc学习笔记19-RESTful支持 概念 REST的样例 controller REST方法的前端控 ...

  10. springMVC 学习笔记(一):springMVC 入门

    springMVC 学习笔记(一):spring 入门 什么是 springMVC springMVC 是 spring 框架的一个模块,springMVC 和 spring 无需通过中间整合层进行整 ...

随机推荐

  1. 风炫安全web安全学习第二十八节课 CSRF攻击原理

    风炫安全web安全学习第二十八节课 CSRF攻击原理 CSRF 简介 跨站请求伪造 (Cross-Site Request Forgery, CSRF),也被称为 One Click Attack 或 ...

  2. 关于ajax 异步文件上传 node 文件后台接口

    <body> <img src="" alt="" id="img"> <input type="f ...

  3. 2021年了,`IEnumerator`、`IEnumerable`还傻傻分不清楚?

    IEnumerator.IEnumerable这两个接口单词相近.含义相关,傻傻分不清楚. 入行多年,一直没有系统性梳理这对李逵李鬼. 最近本人在怼着why神的<其实吧,LRU也就那么回事> ...

  4. 剑指offer 面试题10:斐波那契数列

    题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0).n<=39 编程思想 知道斐波拉契数列的规律即可. 编程实现 class Solu ...

  5. python_字典(dict)

    dict 一.结构: info = { "key":"value", "key":"value" } print(inf ...

  6. 【Java】流程控制 - 顺序结构、 选择(分支)结构(单分支、双分支、多分支、嵌套)、循环结构(for、while、do...while)、跳转语句(break、continue)

    流程控制语句结构 文章目录 流程控制语句结构 一. 顺序结构 1. 输出语句 2. 输入语句 3.code 二.复合语句 三. 分支结构 1. 条件判断 1.单分支结构 2.双分支结构 3.多分支结构 ...

  7. ps -p 进程号

    [root@ma ~]# ps -p 1 PID TTY TIME CMD 1 ? 00:00:01 init

  8. vmstat参数详解

    vmstat 5 可以使用ctrl+c停止vmstat,可以看到输出依赖于所用的操作系统,因此可能需要阅读一下手册来解读报告 第一行的值是显示子系统启动以来的平均值,第二行开始展示现在正在发生的情况, ...

  9. Loadrunner录制脚本与编写脚本的区别

    异同点: 1.录制的和编写的脚本质量上没有区别 2.性能脚本关心的是用户和服务器的数据交互,从这点上来看,录制和编写也没有区别,手动编写脚本也可以写出很真实的脚本 3.能录制的情况下,就录制吧,谁每天 ...

  10. ctfshow—web—web4

    打开靶机 发现与web3很相似,测试文件包含未成功 此题有两种解决方法 一.日志注入 查看日志的默认目录,得到了日志文件 ?url=/var/log/nginx/access.log 进行日志注入 & ...