表单提交的多种方法
表单提交共有四个方法
表单提交方式一:直接利用form表单提交
html页面代码:
Insert title here
controller页面代码:
package com.help.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
@PostMapping("/user")
@ResponseBody
public String add(String username,String password){
System.out.println("返回值是》》"+username+"---->"+password);
String str = "返回值是"+username+password;
return str;
}
}
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
表单提交方式二:使用ajax提交
html页面代码:
Insert title here
controller类的代码不变,启动项目并访问http://localhost:8080/query.html 返货成功即可
这里遇到一个问题:就是传过去数据后,返回值正常,但进入了error ,使用console打印error时出现 parsererror
错误原因:ajax的datatype设置问题 我之前设置为 json ,这种只能是接收后台传回来的json值 传回其他值就会出现这种错误
解决办法:将datatype的类型改为 text
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
表单提交方式三:使用easyui的form插件提交
html页面代码:(需要引入Jquery 与 easyui的js文件)
Insert title here
controller类不变,启动项目并访问http://localhost:8080/query.html 这个是我定义的页面返回结果表示成功
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
表单提交方式四:form表单提交文件/图片
需要设定form的enctype=“multipart/form-data” 如果不加这个会传不过去
附件只能通过submit方法进行提交
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
将form表单的一些属性记在下方以便参考:
Insert title here
原文:https://www.cnblogs.com/sonsmart/p/15265646.html