SpringMVC学习笔记2
一、日期赋值
目标:在springMVC中日期赋值兼容性更广泛
不能直接处理,必须使用转换器
1、定义转换器,实现接口Converter<From,To>
- package com.zy.converter;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.springframework.core.convert.converter.Converter;
- //从字符串转日期
- public class MyDateConverter implements Converter<String, Date> {
- @Override
- public Date convert(String value) {//参数就是传入的字符串日期
- //1999年6月6日
- //1999-6-6
- //1999.6.6
- //1999/6/6 默认支持
- // 第一种为例
- //1创建相对应的日期格式化对象
- SimpleDateFormat simpleDateFormat = null;
- try {
- if(value.contains("年")){
- simpleDateFormat= new SimpleDateFormat("yyyy年MM月dd日");
- }else if(value.contains("-")){
- simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd");
- }else if(value.contains(".")){
- simpleDateFormat= new SimpleDateFormat("yyyy.MM.dd");
- }else if(value.contains("/")){
- simpleDateFormat= new SimpleDateFormat("yyyy/MM/dd");
- }
- //2把字符串解析成一个日期对象
- Date parse = simpleDateFormat.parse(value);
- //3返回结果
- return parse;
- } catch (ParseException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
- }
2、配置
spring.xml中
- <!-- 2配置日期转换器 -->
- <bean id="formattingConversion" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
- <property name="converters">
- <list>
- <!-- 配置自己的转换器 一个bean就是一个类-->
- <bean class="com.zy.converter.MyDateConverter"></bean>
- </list>
- </property>
- </bean>
- <!-- 3引用上文的转换器 -->
- <mvc:annotation-driven conversion-service="formattingConversion"></mvc:annotation-driven>
3、Upload上传
1)导包
2)多功能表单
- <form action="file/up" method="post" enctype="multipart/form-data"><!-- 多功能表单 -->
- 头像:<input type="file" name="myfile"/><input type="submit"/>
- </form>
3)文件上传解析器
spring.xml
- <!-- 4文件上传解析器 id名为multipartResolver,不然可能会报错-->
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <property name="maxUploadSize">
- <value>5242880</value>
- </property>
- </bean>
4)辅助方法+上传文件
- @Controller
- @RequestMapping("/file")
- public class FileController {
- //辅助方法
- //1根据逻辑路径得到真实路径
- //过期的
- //@SuppressWarnings(“deprecation”)表示不检测过期的方法
- @SuppressWarnings("deprecation")
- public String myGetRealPath(String path,HttpServletRequest request){
- String realPath = request.getRealPath(path);
- System.out.println("真实路径:"+realPath);
- File file = new File(realPath);
- if(!file.exists()){
- file.mkdirs();
- }
- return realPath;
- }
- //2更改文件名
- public String newFileName(MultipartFile file){
- String originalFilename = file.getOriginalFilename();
- //abc.jpg
- //截取后缀,拼接新的文件名
- //后缀
- String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
- //新文件名要求:上传中防止文件名重复,发生覆盖
- String uu = UUID.randomUUID().toString();
- String newName=uu+substring;
- return newName;
- }
- // @Test
- // public void test(){
- // System.out.println(UUID.randomUUID());
- // }
- //上传--//如果controller只需要跳转页面的话,可以把返回值写成String 不用写成ModelAndView
- @SuppressWarnings("resource")
- @RequestMapping("/up")
- public String up(MultipartFile myfile,HttpServletRequest request) throws Exception{
- //得到真实路径 <!--tomcat服务器来给该request赋值-->
- String path="/img";//逻辑路径
- String myGetRealPath = myGetRealPath(path, request);
- //得到新的文件名
- String newFileName = newFileName(myfile);
- //上传----把本地文件按流的方式copy到服务器上
- //输入流
- InputStream is = myfile.getInputStream();
- //输出流
- FileOutputStream os = new FileOutputStream(myGetRealPath+"/"+newFileName);
- //copy
- IOUtils.copy(is, os);
- request.setAttribute("img",path+"/"+newFileName);
- os.close();
- is.close();
- return "/index.jsp";
- }
4、下载
- //图片下载
- @SuppressWarnings("resource")
- @RequestMapping("/down")
- public void down(HttpServletResponse response,String fileName,HttpServletRequest request) throws Exception {
- //设置头--[下载attachment/预览]
- response.setHeader("Content-Disposition", "attachment;fileName="+fileName);
- //下载的本质--文件按流的方式从服务器copy到本地
- //得到资源在服务器的真实路径
- String path="/aaa/"+fileName;
- String myGetRealPath = myGetRealPath(path, request);
- FileInputStream fileInputStream = new FileInputStream(myGetRealPath);
- ServletOutputStream outputStream = response.getOutputStream();
- IOUtils.copy(fileInputStream, outputStream);
- fileInputStream.close();
- outputStream.close();
- //下载以后不要跳页面
- }
5、ModeAndView
- @RequestMapping("/go")
- public ModelAndView go(){
- ModelAndView modelAndView = new ModelAndView();
- //modelAndView分为两个功能-----我们以前见过的
- //model 数据
- //view 视图
- //------
- //存域
- modelAndView.addObject("mydata","880");
- //跳转
- modelAndView.setViewName("/abc.jsp");
- return modelAndView;
- }
6、视图解析器(便利性)
spring.xml
- <!-- 5视图解析器 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->- <property name="prefix" value="/WEB-INF/view/"/>
- <!-- 后缀 -->
<property name="suffix" value=".jsp"/>- </bean>
- @RequestMapping("/show")
- public String show(){
- //带视图解析器的跳转
- // /WEB-INF/view/
- // .jsp
- return "aaa";
- }
- @RequestMapping("/mmm")
- public String mmm(){
- return "forward:/WEB-INF/mmm/bbb.jsp";//指定响应方式可以摆脱视图解析器
- }
- //----
- ///WEB-INF下的页面不能通过重定向到
- //return "forward:/WEB-INF/mmm/bbb.jsp" 转发
- //return "redirect:bbb.jsp" 重定向
- <hr>
- 文件上传
- <form action="${pageContext.request.contextPath}/file/up.action" method="post" enctype="multipart/form-data"><!-- 多功能表单 -->
- 头像:<input type="file" name="myfile"/><input type="submit"/>
- </form>
- <img alt="" src="${pageContext.request.contextPath}${img}">
- </body>
- 文件下载
- <a href="${pageContext.request.contextPath}/file/down.action?fileName=110.jpg">下载</a>
- <a href="${pageContext.request.contextPath}/file/go.action?">跳转</a>
- <a href="${pageContext.request.contextPath}/file/show.action?">带视图解析器跳转</a>
- <a href="${pageContext.request.contextPath}/file/mmm.action?">带视图解析器跳转2</a>
6、url-pattern
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <display-name>springmvc01</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- <!-- 配置前端控制器 -->
- <servlet>
- <servlet-name>aaa</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <!-- 加载配置文件 -->
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring.xml</param-value><!-- 配置文件的位置 classpath代表src -->
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>aaa</servlet-name>
- <url-pattern>*.action</url-pattern><!-- 拦截规则 --><!-- 后缀拦截 拦截以action结尾的请求-->
- </servlet-mapping>
- <!-- / /* /*范围更广,包括jsp的拦截 -->
- <!-- 配置编码过滤器 -->
- <filter>
- <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
SpringMVC学习笔记2的更多相关文章
- 史上最全的SpringMVC学习笔记
SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...
- springmvc学习笔记--REST API的异常处理
前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...
- springmvc学习笔记---面向移动端支持REST API
前言: springmvc对注解的支持非常灵活和飘逸, 也得web编程少了以往很大一坨配置项. 另一方面移动互联网的到来, 使得REST API变得流行, 甚至成为主流. 因此我们来关注下spring ...
- SpringMVC:学习笔记(8)——文件上传
SpringMVC--文件上传 说明: 文件上传的途径 文件上传主要有两种方式: 1.使用Apache Commons FileUpload元件. 2.利用Servlet3.0及其更高版本的内置支持. ...
- springmvc学习笔记(简介及使用)
springmvc学习笔记(简介及使用) 工作之余, 回顾了一下springmvc的相关内容, 这次也为后面复习什么的做个标记, 也希望能与大家交流学习, 通过回帖留言等方式表达自己的观点或学习心得. ...
- springmvc学习笔记(常用注解)
springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...
- SpringMVC学习笔记之二(SpringMVC高级参数绑定)
一.高级参数绑定 1.1 绑定数组 需求:在商品列表页面选中多个商品,然后删除. 需求分析:功能要求商品列表页面中的每个商品前有一个checkbok,选中多个商品后点击删除按钮把商品id传递给Cont ...
- springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定
springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定 标签: springmvc springmvc学习笔记13-springmvc注解开发之集合类型參数绑定 数组绑定 需 ...
- springmvc学习笔记(19)-RESTful支持
springmvc学习笔记(19)-RESTful支持 标签: springmvc springmvc学习笔记19-RESTful支持 概念 REST的样例 controller REST方法的前端控 ...
- springMVC 学习笔记(一):springMVC 入门
springMVC 学习笔记(一):spring 入门 什么是 springMVC springMVC 是 spring 框架的一个模块,springMVC 和 spring 无需通过中间整合层进行整 ...
随机推荐
- 风炫安全web安全学习第二十八节课 CSRF攻击原理
风炫安全web安全学习第二十八节课 CSRF攻击原理 CSRF 简介 跨站请求伪造 (Cross-Site Request Forgery, CSRF),也被称为 One Click Attack 或 ...
- 关于ajax 异步文件上传 node 文件后台接口
<body> <img src="" alt="" id="img"> <input type="f ...
- 2021年了,`IEnumerator`、`IEnumerable`还傻傻分不清楚?
IEnumerator.IEnumerable这两个接口单词相近.含义相关,傻傻分不清楚. 入行多年,一直没有系统性梳理这对李逵李鬼. 最近本人在怼着why神的<其实吧,LRU也就那么回事> ...
- 剑指offer 面试题10:斐波那契数列
题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0).n<=39 编程思想 知道斐波拉契数列的规律即可. 编程实现 class Solu ...
- python_字典(dict)
dict 一.结构: info = { "key":"value", "key":"value" } print(inf ...
- 【Java】流程控制 - 顺序结构、 选择(分支)结构(单分支、双分支、多分支、嵌套)、循环结构(for、while、do...while)、跳转语句(break、continue)
流程控制语句结构 文章目录 流程控制语句结构 一. 顺序结构 1. 输出语句 2. 输入语句 3.code 二.复合语句 三. 分支结构 1. 条件判断 1.单分支结构 2.双分支结构 3.多分支结构 ...
- ps -p 进程号
[root@ma ~]# ps -p 1 PID TTY TIME CMD 1 ? 00:00:01 init
- vmstat参数详解
vmstat 5 可以使用ctrl+c停止vmstat,可以看到输出依赖于所用的操作系统,因此可能需要阅读一下手册来解读报告 第一行的值是显示子系统启动以来的平均值,第二行开始展示现在正在发生的情况, ...
- Loadrunner录制脚本与编写脚本的区别
异同点: 1.录制的和编写的脚本质量上没有区别 2.性能脚本关心的是用户和服务器的数据交互,从这点上来看,录制和编写也没有区别,手动编写脚本也可以写出很真实的脚本 3.能录制的情况下,就录制吧,谁每天 ...
- ctfshow—web—web4
打开靶机 发现与web3很相似,测试文件包含未成功 此题有两种解决方法 一.日志注入 查看日志的默认目录,得到了日志文件 ?url=/var/log/nginx/access.log 进行日志注入 & ...