几种实现表单提交的方法
在form标签中添加Action(提交的地址)和method(post),且有一个submit按钮
(<input type='submit'>)就可以进行数据的提交,每一个input标签都需要有一个name属性,才能进行提交
当点击登陆时,向数据库发生的数据是:username=username&password=password.
这种默认的提交方式,一般会进行页面的跳转(不成功时跳转到当前页面)。而有时候我们是对弹出框进行数据提交的,希望提交成功则关闭弹出框并刷选父页面,失败则提示失败原因,且弹出框不关闭。此时可以采用Ajax进行数据提交.
具体参考第四种方案
无刷新页面提交表单
表单可实现无刷新页面提交,无需页面跳转,如下,通过一个隐藏的iframe实现,form表单的target设置为iframe的name名称,
form提交目标位当前页面iframe则不会刷新页面
1
2
3
4
|
<form action = "/url.do" method= "post" target= "targetIfr" >
<input type= "text" name = "name" />
</form> <iframe name = "targetIfr" style="display:none" ></iframe>
|
通过type=submit提交
一般表单提交通过type=submit实现,input type=”submit”,浏览器显示为button按钮,通过点击这个按钮提交表单数据跳转到/url.do
1
2
3
4
|
<form action = "/url.do" method= "post" >
<input type= "text" name = "name" />
<input type= "submit" value= "提交" >
</form> |
js提交form表单
js事件触发表单提交,通过button、链接等触发事件,js调用submit()方法提交表单数据,jquery通过submit()方法
1
2
3
4
5
6
7
8
9
10
|
<form id= "form" action = "/url.do" method= "post" >
<input type= "text" name = "name" />
</form> <script> document.getElementById( "form" ).submit();
jquery: $( "#form" ).submit();
</script> |
ajax异步提交表单数据
采用ajax异步方式,通过js获取form中所有input、select等组件的值,将这些值组成Json格式,通过异步的方式与服务器端进行交互,一般将表单数据传送给服务器端,服务器端处理数据并返回结果信息等
1
2
3
4
5
6
7
8
9
10
11
12
|
<form id= "form" method= "post" >
<input type= "text" name = "name" id= "name" />
</form> var params = { "name" , $( "#name" ).val()}
$.ajax({
type: "POST" ,
url: "/url.do" ,
data: params,
dataType : "json" ,
success: function (respMsg){
}
});
|
此时可以在callback函数中对请求结果进行判断,然后执行不同的动作(页面跳转或刷选数据、提醒错误都可以)
页面无跳转
如果通过form表单提交请求服务端去下载文件,这时当前页面不会发生跳转,服务端返回void,通过response 去写文件数据,页面会显示下载文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<form action = "/url.do" method= "post" >
<input type= "text" name = "name" />
<input type= "submit" value= "提交" >
</form> @RequestMapping(value = "/url" )
public void exportFile(HttpServletRequest req, HttpServletResponse response, String rptId)
throws Exception {
OutputStream out = null ;
try {
String rptName = "file" ;
String fileName = new String((rptName + excelAble.getFileSuffix()).getBytes( "GBK" ),
"8859_1" );
response.reset();
response.setContentType( "application/octec-stream" );
response.setHeader( "Content-disposition" , "attachment; filename=" + fileName);
out = response.getOutputStream();
excelAble.exportFile( out );
} catch (Exception e) {
logger.error(e);
} finally {
if ( out != null ) {
out . close ();
}
}
}
|
form表单上传文件
使用form表单进行上传文件需要为form添加enctype=”multipart/form-data” 属性,除此之外还需要将表单的提交方法改成post,如下 method=”post”, input type的类型需要设置为file
1
2
3
4
|
<form action = "/url.do" enctype= "multipart/form-data" method= "post" >
<input type= "file" name = "name" />
<input type= "submit" value= "提交" >
</form>
|
附件只能通过submit方法进行提交,
推荐阅读
-
在 ts 中实现类 java hashmap 的简单方法
-
[阅读笔记 - 解密超大规模集成电路设计方法] 问题 6:实现超大规模集成电路(VLSI)设计的主要方法是什么?
-
在 Spring Boot 中正确注入具有多个实现的接口的六种方法
-
Java 混淆的几种方法
-
[JavaEE 入门] 深入了解多线程阻塞队列的工作原理、生产者-消费者模型的实现方法以及服务器崩溃的原因!!!!!
-
将YUV420图像转换为BGR图像的方法使用OpenCvSharp实现
-
两种方法将YUV420P文件转换为PNG图像格式的Python实现
-
利用 GLSurfaceView 创建 OpenGLES 环境的实现方法
-
快速实现可运行的2048:一个经典游戏的键码方法
-
Java 8新特性探究(十三)JavaFX 8新特性以及开发2048游戏-JavaFX历史## 跟java在服务器端和web端成绩相比,桌面一直是java的软肋,于是Sun公司在2008年推出JavaFX,弥补桌面软件的缺陷,请看下图JavaFX一路走过来的改进 从上图看出,一开始推出时候,开发者需使用一种名为JavaFX Script的静态的、声明式的编程语言来开发JavaFX应用程序。因为JavaFX Script将会被编译为Java bytecode,程序员可以使用Java代码代替。 JavaFX 2.0之后的版本摒弃了JavaFX Script语言,而作为一个Java API来使用。因此使用JavaFX平台实现的应用程序将直接通过标准Java代码来实现。 JavaFX 2.0 包含非常丰富的 UI 控件、图形和多媒体特性用于简化可视化应用的开发,WebView可直接在应用中嵌入网页;另外 2.0 版本允许使用 FXML 进行 UI 定义,这是一个脚本化基于 XML 的标识语言。 从JDK 7u6开始,JavaFx就与JDK捆绑在一起了,JavaFX团队称,下一个版本将是8.0,目前所有的工作都已经围绕8.0库进行。这是因为JavaFX将捆绑在Java 8中,因此该团队决定跳过几个版本号,迎头赶上Java 8。 ##JavaFx8的新特性 ## ###全新现代主题:Modena 新的Modena主题来替换原来的Caspian主题。不过在Application的start方法中,可以通过setUserAgentStylesheet(STYLESHEET_CASPIAN)来继续使用Caspian主题。 参考http://fxexperience.com/2013/03/modena-theme-update/ ###JavaFX 3D 在JavaFX8中提供了3D图像处理API,包括Shape3D (Box, Cylinder, MeshView, Sphere子类),SubScene, Material, PickResult, LightBase (AmbientLight 和PointLight子类),SceneAntialiasing等。Camera类也得到了更新。从JavaDoc中可以找到更多信息。 ###富文本 强化了富文本的支持 ###TreeTableView ###日期控件DatePicker 增加日期控件 ###用于 CSS 结构的公共 API