JAVA入门[16]-form表单,上传文件
一、如何传递参数
使用 @RequestParam 可以传递查询参数。例如:http://localhost:8092/category/detail?id=1
@RequestMapping("/detail")
public String detail(@RequestParam("id") int id,Model model){
Category category=new Category();
category.setCateId(id);
category.setCateName("测试分类"+id);
model.addAttribute("cate",category); return "detail.html";
}
使用 @PathVariable可以传递路径参数。例如:http://localhost:8092/category/edit/1
@RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
public String edit(@PathVariable("id") int id,Model model) {
//todo:get category from db
Category category=new Category();
category.setCateId(id);
category.setCateName("测试分类"+id);
model.addAttribute("cate",category); return "edit.html";
}
二、校验表单
1.首先定义实体类。
public class Category{
public Category(){} @NotNull
@Min(1)
private int cateId; @NotNull
private String cateName; public int getCateId() {
return cateId;
} public void setCateId(int cateId) {
this.cateId = cateId;
} public String getCateName() {
return cateName;
} public void setCateName(String cateName) {
this.cateName = cateName;
}
}
2.表单edit.html
<form method="post" th:object="${cate}" th:action="@{/category/save}" enctype="multipart/form-data">
<table>
<tr>
<td>id:</td>
<td><input type="text" th:field="*{cateId}"></td>
</tr>
<tr>
<td>name:</td>
<td><input type="text" th:field="*{cateName}"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
3.通过给action方法的参数添加@Valid注解,这会告知Spring,需要确保这个对象满足校验限制
@RequestMapping(value = "/save",method = RequestMethod.POST)
public String save( @Valid Category category, Errors errors) throws IOException {...}
错误可以通过Errors对象进行访问,现在这个对象已作为processRegistration()方法的参数。(很重要一点需要注意,Errors参数要紧跟在带有@Valid注解的参数后面,@Valid注解所标注的就是要检验的参数。
三、上传图片
1.设置web.xml配置
web.xml配置multipart-config
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<multipart-config>
<location></location>
<max-file-size>2097152</max-file-size>
<max-request-size>4194304</max-request-size>
</multipart-config>
</servlet>
2.from表单
form要将enctype属性设置为multipart/form-data,这就告诉浏览器以multipart数据的形式提交表单
input标签要把type设置为file,这能够让用户选择要上传的图片文件。accept属性用来将文件类型限制为JPEG、PNG以及GIF图片。根据其name属性,图片数据将会发送到multipart请求中的profilePicture part之中
<form method="post" th:object="${cate}" th:action="@{/category/save}" enctype="multipart/form-data">
<table>
<tr>
<td>id:</td>
<td><input type="text" th:field="*{cateId}"></td>
</tr>
<tr>
<td>name:</td>
<td><input type="text" th:field="*{cateName}"></td>
</tr>
<tr>
<td>file:</td>
<td>
<input type="file" accept="image/jpeg,image/png,image/jpg" name="picture">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
3.controller:
@RequestPart :图片对应的参数要添加该注解
spring提供了Multipart MultipartFile对象,它为处理multipart数据提供了内容更为丰富的对象
transferTo() ,它能够帮助我们将上传的文件写入到文件系统中
@RequestMapping(value = "/save",method = RequestMethod.POST)
public String save(@RequestPart("picture") MultipartFile picture, @Valid Category category, Errors errors) throws IOException { //todo:save file to image server
String filepath=request.getRealPath("/")+"upload/"+picture.getOriginalFilename();
picture.transferTo(new File(filepath)); if(errors.hasErrors()){
return "edit.html";
}
//todo:save category to db
return "redirect:/category/detail?id="+category.getCateId();
}
JAVA入门[16]-form表单,上传文件的更多相关文章
- Java如何解决form表单上传文件,以及页面返回处理结果通知!
前端JSP代码 <form id='formSumbit' class='form-horizontal' action='/ncpay/route/chlsubmcht/batchImpor' ...
- django 基于form表单上传文件和基于ajax上传文件
一.基于form表单上传文件 1.html里是有一个input type="file" 和 ‘submit’的标签 2.vies.py def fileupload(request ...
- 巨蟒python全栈开发django11:ajax&&form表单上传文件contentType
回顾: 什么是异步? 可以开出一个线程,我发出请求,不用等待返回,可以做其他事情. 什么是同步? 同步就是,我发送出了一个请求,需要等待返回给我信息,我才可以操作其他事情. 局部刷新是什么? 通过jq ...
- vue form表单上传文件
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js">< ...
- 使用form表单上传文件
在使用form表单上传文件时候,input[type='file']是必然会用的,其中有一些小坑需要避免. 1.form的 enctype="multipart/form-data" ...
- JsonResponse类的使用、form表单上传文件补充、CBV和FBV、HTML的模板语法之传值与过滤器
昨日内容回顾 Django请求生命周期 # 1.浏览器发起请求 到达Django的socket服务端(web服务网关接口) 01 wsgiref 02 uwsgi + nginx 03 WSGI协议 ...
- nodejs 模拟form表单上传文件
使用nodejs来模拟form表单进行文件上传,可以同时上传多个文件. 以前项目里有这个方法,最近在客户那里出问题了,同事说,这个方法从来就没管用过,SO,用了一天时间把这个方法给搞出来了(觉得花费的 ...
- form表单上传文件使用multipart请求处理
在开发Web应用程序时比较常见的功能之一,就是允许用户利用multipart请求将本地文件上传到服务器,而这正是Grails的坚固基石——spring MVC其中的一个优势.Spring通过对Serv ...
- PHP 后台程序配置config文件,及form表单上传文件
一,配置config文件 1获取config.php文件数组, 2获取form 表单提交的值 3保存更新config.php文件,代码如下: $color=$_POST['color']; $back ...
- 通过form表单上传文件获取后台传来的数据
小伙伴是不是遇到过这样的问题,通过submit提交form表单的时候,不知怎么获取后台传来的返回值.有的小伙伴就会说你不会发送ajax,其实也会.假如提交的form表单中含有文件,怎么办? 步骤1:想 ...
随机推荐
- jquery的img的动态title换行
因业务需要,需要在图片悬浮的时候显示动态加载的title,如果图片的title为固定的可以使用以下方式换行: <img src="1.jpg" title="第一行 ...
- 在为知笔记中使用Markdown和思维导图
为知笔记Wiz是一款很好的网摘和笔记工具,作为为知的忠实用户,我在为知收费后第一时间就购买了两年的授权,毕竟这么多年积累的资料都在为知上,我也习惯了使用Wiz来做些工作相关的笔记.为知笔记自带Mark ...
- tomcat相关实验
tomcat相关实验 1.实现LNT 同主机实现 1.安装并启动tomcat 1)OpenJDK的安装 yum install java-1.8.0-openjdk-devel.x86_64 确定JD ...
- 深度学习之seq2seq模型以及Attention机制
RNN,LSTM,seq2seq等模型广泛用于自然语言处理以及回归预测,本期详解seq2seq模型以及attention机制的原理以及在回归预测方向的运用. 1. seq2seq模型介绍 seq2se ...
- 翻译连载 | 第 11 章:融会贯通 -《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇
原文地址:Functional-Light-JS 原文作者:Kyle Simpson-<You-Dont-Know-JS>作者 关于译者:这是一个流淌着沪江血液的纯粹工程:认真,是 HTM ...
- 使用GetThumbnailImage进行图片缩放操作
/// <summary> /// 获取等比例缩放图片的方法 /// </summary> /// <param name=&q ...
- LKD: Chapter 8 Bottom Halves and Deferring Work
In 2.6.x, there are 3 mechanisms for implementing a bottom half: softirqs, tasklets and work queues. ...
- 移动端页面input输入框被键盘遮挡问题
<body class="layout-fixed"> <!-- fixed定位的头部 --> <header> </header> ...
- 一个for循环打印二维数组
#include<stdio.h> #define MAXX 2 #define MAXY 3 void printarray() { ,,,,,}; ;i< MAXX*MAXY;i ...
- Linux 链接详解(1)
可执行文件的生成过程: hello.c ----预处理---> hello.i ----编译----> hello.s -----汇编-----> hello.o -----链接- ...