一、如何传递参数

使用 @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. Android开发之漫漫长途 Ⅱ——Activity的显示之Window和View(1)

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  2. 前后端分手大师——MVVM 模式

    之前对 MVVM 模式一直只是模模糊糊的认识,正所谓没有实践就没有发言权,通过这两年对 Vue 框架的深入学习和项目实践,终于可以装B了有了拨开云雾见月明的感觉. 简而言之 Model–View–Vi ...

  3. 【基础】Attribute的妙用

    一.何为Attribute 下面是微软官方对Attribute的解释: 公共语言运行时允许你添加类似关键字的描述声明,叫做Attributes,它对程序中的元素进行标注,如类型.字段.方法和属性等.A ...

  4. nginx的5个特点

    nginx的5个特点(2017/11/16 白杨整理) 1.动静分离 Nginx是一种轻量级,高性能,多进程的Web服务器,非常适合作为静态资源的服务器使用,而动态的访问操作可以使用稳定的Apache ...

  5. c#中的Out, params,ref 细说并沉淀

    1. Out,params,ref之前先记录平时用的最多的按值传递参数的情况,当然默认情况下参数传入函数的默认行为也是按值传递的. 1: //默认情况下参数会按照值传递 2: static int a ...

  6. Python之signal模块

    http://www.cnblogs.com/dkblog/archive/2011/03/07/1980636.html 1.超时处理 #!/usr/bin/env python2.7 #-*- c ...

  7. 【转】jQuery代码片段备用

    在CSDN看到的,记下备用.原文:http://www.csdn.net/article/2013-07-16/2816238-15-jquery-code-snippets-for-develope ...

  8. [C#源代码]使用SCPI指令对指定通信端口(RS232/USB/GPIB/LAN)的仪器编程

    本文为原创文章,源代码为原创代码,如转载/复制,请在网页明显位置标明原文名称.作者及网址,谢谢! 本软件是基于NI-VISA/VISA32(Virtual Instrument Software Ar ...

  9. Fielddata is disabled on text fields by default. Set fielddata=true on [gender] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memor

    ES进行如下聚合操作时,会报如题所示错误: ➜ Downloads curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "size ...

  10. c语言的枚举(遍历枚举)与数据类型总结

    一.枚举的概念 枚举是C语言中的一种基本数据类型,并不是构造类型,它可以用于声明一组常数.当一个变量有几个固定的可能取值时,可以将这个变量定义为枚举类型. 比如,你可以用一个枚举类型的变量来表示季节, ...