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:想 ...
随机推荐
- 记一次list循环删除元素的突发事件!
事情是这样的,由于想再回顾一下基础,就写了一个main函数,里面循环删元素的代码.如下: List<String> a = new ArrayList<String>(); a ...
- 检测应用的内存泄漏情况(shell)
写代码--调试--修BUG 改来改去可能还存在一些没发现的问题,在工程量大的时候更容易出现,例如内存泄漏这样的问题,严重影响着系统性能. 网上有些检测C程序是否存在内存泄漏的工具还不错的,例如valg ...
- 重构手法之Introduce Explaining Variable(引用解释性变量)
返回总目录 6.5Introduce Explaining Variable(引用解释性变量) 概要 你有一个复杂的表达式. 将该复杂表达式(或其中一部分)的结果放进一个临时变量,以此变量名称来解释表 ...
- Hibernate从零开始的反向工程
首先 创建一个web项目 导入jar包 Bulid Path 先现在hibernate的插件 help-->eclipse marketplace-->输入tool 点instal ...
- 非关系型数据库redis-java基本操作
概述 redis是一个key-value的nosql数据库(非关系型数据库).支持存储的value类型包括string(字符串).list(链表).set(集合).zset(sorted set -- ...
- KNN--用于手写数字识别
优点:精度高,对异常值不敏感,无数据输入假定 缺点:计算复杂度高,空间复杂度高 适用数据范围:数值型和标称型 一般流程: (1). 收集数据(网络抓取) (2).处理数据,将数据处 ...
- 项目实战5—企业级缓存系统varnish应用与实战
企业级缓存系统varnish应用与实战 环境背景:随着公司业务快速发展,公司的电子商务平台已经聚集了很多的忠实粉丝,公司也拿到了投资,这时老板想通过一场类似双十一的活动,进行一场大的促销,届时会有非常 ...
- struct 类型指针技巧
此类型对于两种结构的查找和删除都十分方便,实际项目中也常常用到.
- 这些 Drawable 的小技巧,你都了解吗?
一.前言 在 Android 的开发过程中,Drawable 经常会被用到,一般会用 Drawable 为 View 设置一个显示的效果.而在 Android 下,也提供了很多 Drawable 的默 ...
- RGBA 和 opacity的区别
两者都可以设置透明度 区别 RGBA 只影响当前元素 opacity 后代会继承该css 值,暂时还没有办法清除该css 在线演示