一、如何传递参数

使用 @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表单,上传文件的更多相关文章

  1. Java如何解决form表单上传文件,以及页面返回处理结果通知!

    前端JSP代码 <form id='formSumbit' class='form-horizontal' action='/ncpay/route/chlsubmcht/batchImpor' ...

  2. django 基于form表单上传文件和基于ajax上传文件

    一.基于form表单上传文件 1.html里是有一个input type="file" 和 ‘submit’的标签 2.vies.py def fileupload(request ...

  3. 巨蟒python全栈开发django11:ajax&&form表单上传文件contentType

    回顾: 什么是异步? 可以开出一个线程,我发出请求,不用等待返回,可以做其他事情. 什么是同步? 同步就是,我发送出了一个请求,需要等待返回给我信息,我才可以操作其他事情. 局部刷新是什么? 通过jq ...

  4. vue form表单上传文件

    <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js">< ...

  5. 使用form表单上传文件

    在使用form表单上传文件时候,input[type='file']是必然会用的,其中有一些小坑需要避免. 1.form的 enctype="multipart/form-data" ...

  6. JsonResponse类的使用、form表单上传文件补充、CBV和FBV、HTML的模板语法之传值与过滤器

    昨日内容回顾 Django请求生命周期 # 1.浏览器发起请求 到达Django的socket服务端(web服务网关接口) 01 wsgiref 02 uwsgi + nginx 03 WSGI协议 ...

  7. nodejs 模拟form表单上传文件

    使用nodejs来模拟form表单进行文件上传,可以同时上传多个文件. 以前项目里有这个方法,最近在客户那里出问题了,同事说,这个方法从来就没管用过,SO,用了一天时间把这个方法给搞出来了(觉得花费的 ...

  8. form表单上传文件使用multipart请求处理

    在开发Web应用程序时比较常见的功能之一,就是允许用户利用multipart请求将本地文件上传到服务器,而这正是Grails的坚固基石——spring MVC其中的一个优势.Spring通过对Serv ...

  9. PHP 后台程序配置config文件,及form表单上传文件

    一,配置config文件 1获取config.php文件数组, 2获取form 表单提交的值 3保存更新config.php文件,代码如下: $color=$_POST['color']; $back ...

  10. 通过form表单上传文件获取后台传来的数据

    小伙伴是不是遇到过这样的问题,通过submit提交form表单的时候,不知怎么获取后台传来的返回值.有的小伙伴就会说你不会发送ajax,其实也会.假如提交的form表单中含有文件,怎么办? 步骤1:想 ...

随机推荐

  1. AutoMapper IIS回收引发的 未将对象引用设置到对象实例

    一.前言 最近使用AutoMapper的时候,一段时间久会产生System.NullReferenceException:未将对象引用设置到对象的实例.这个问题.后来通过测试,发现部署在IIS上的站点 ...

  2. 如何将一个div水平垂直居中?4种方法做推荐

    方案一: div绝对定位水平垂直居中[margin:auto实现绝对定位元素的居中], 兼容性:,IE7及之前版本不支持 div{ width: 200px; height: 200px; backg ...

  3. 通过 Visual Studio 的“代码度量值”来改进代码质量

    1 软件度量值指标 1.1 可维护性指数 表示源代码的可维护性,数值越高可维护性越好.该值介于0到100之间.绿色评级在20到100之间,表明该代码具有高度的可维护性:黄色评级在10到19之间,表示该 ...

  4. Spring JDBC 示例

    在使用普通的 JDBC 数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等.但 Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQL 语句,处理异常, ...

  5. 为什么ERP行业发展缓慢规模难扩大?

    题记:这个行业只会越来越好,但是能沉淀下来做事儿的企业越来越少,因为收益真的很漫长:能够真正进入这个行业难,出去也难... 经常在知乎上看到类似的问题:既然所有ERP系统都很烂,那有没有创业公司的空间 ...

  6. listview相关代码整理

    虽然listview已经慢慢被替代了,  不过还是整理下 , 留作纪念吧 /** * 获取 listview 实际滚动的距离. [ 相对于listview的第一个项目左上角.] * * @return ...

  7. SQL学习入门(一) 概述

    从事开发也有一段时间了,期间接触SQL也挺长时间了.许多东西也不是理解的特别深刻,所以写写随笔总结归纳一下. 如有不对的地方,欢迎指正! 一.认识SQL (1) 什么是SQL? 1.SQL 指结构化查 ...

  8. bootstrap_table_class表格样式实例

    <div class="container"> <h2>表格</h2> <!--table标签级别的样式说明如下: ----.table ...

  9. C#Winform窗体 DataGridView全选按钮的实现方式

    最近,在做CS程序遇到一个头疼的问题,datagridview列表的全选按钮遇到各种问题,做的是自适应窗体大小,当窗体最大化导致全选按钮出现与列表数据不一致,特别不搭配,试了很久,网上也找了好多资料各 ...

  10. 使用OpenCV进行网络摄像头的图像采集及视频存储

    rtspURL格式 rtsp://[username]:[password]@[ip]:[port]/[codec]/[channel]/[subtype]/av_stream 1) username ...