form表单中不需要写action的路径,需要给form表单一个唯一的id,将你要提交的信息的表单中的标签name="action中的javabean对象.javabean属性"。给button按钮添加一个onclick()点击事件,并实现该点击事件,在该onclick()方法中通过ajax将form表单中的数据提交给action层

JSP页面中的代码:
1 <form id="handleform">
<!-- 根据学生id修改学生信息 -->
<input type="hidden" name="student.stuid"/><!-- 隐藏学生id -->
<div class="input-group el_modellist" role="toolbar">
<span class="el_spans">要修改的班级:</span>
<select class="selectpicker form-control" name="student.className" id="fmchechunit" title="请选择">
<option value="0">--请选择班级--</option>
<option value="1">软件一班</option>
<option value="2">软件二班</option>
</select>
</div>
<span class="el_spans">学生姓名:</span>
<input type="text" id="student.name"/>
<div class="input-group el_modellist" role="toolbar">
<span class="el_spans">学生详细信息:</span>
<textarea id="studentMsg" class="form-control texta" rows="10" name="student.msg"></textarea>
</div> <div class="modal-footer">
<button id="submitButton" onclick="saveButton()" type="button" class="btn btn-primary">更新</button>
</div>
</form>
<script type="text/javascript">
function saveButton(){
//通过ajax异步将数据发送给action层
$.ajax({
url : '${pageContext.request.contextPath}/stu/stu_upstudent.action',//这里写上你的action路径
data : $("#handleform").serialize(),//将你在form表单上提交的数据序列化
type : 'POST', //提交方式
dataType : 'json', //提交的数据类型
async:true, //是否异步
success : function(data) {//这是个回调函数 data表示从action中传过来的json数据
//弹出从action层传过来的json格式的数据(用来显示是否更新成功)
alert(data.result);
}
});
}
</script>
action层中的代码:
1 @Controller
@Scope("prototype")
// 控制层,多例模式
public class DangerAction extends ActionSupport { private Student student;
public void setStudent(Student student){
this.student = student;
}
public Student getStudent(){
return this.student;
} @Resource
private StudentService studentService;
public StudentService getStudentService() {
return studentService;
}
public void setStudentService(StudentService studentService) {
this.studentService = studentService;
}
public String updateStudent throws Exception{ boolean flag = studentService.update(student);
HttpServletResponse response = ServletActionContext.getResponse();      //通过json对象将修改反馈信息响应给jsp
JSONObject json = new JSONObject();
if (flag) {
System.out.println(flag);
json.put("result", "修改成功");
} else {
System.out.println(flag);
json.put("result", "修改失败");
}
System.out.println(json.toString());
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(json.toString());
return null;//如果不需要跳转页面就写上null,如果要跳转页面就自己另外写上
}
}
javabean代码: 
1 public class Student{
private int stuid;
private int className;
private int name;
private String studentMsg;
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public int getClassName() {
return className;
}
public void setClassName(int className) {
this.className = className;
}
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
public String getStudentMsg() {
return studentMsg;
}
public void setStudentMsg(String studentMsg) {
this.studentMsg = studentMsg;
} }

通过button将form表单的数据提交到action层的更多相关文章

  1. form表单Get方式提交时,action中带参数传递不了

    <form action="getPostServlet/getPost.do?param4=param4" method="get"> <i ...

  2. django做form表单的数据验证

    我们之前写的代码都没有对前端input框输入的数据做验证,我们今天来看下,如果做form表单的数据的验证 在views文件做验证 首先用文字描述一下流程 1.在views文件中导入forms模块 2. ...

  3. jquery序列化form表单使用ajax提交后处理返回的json数据

    1.返回json字符串: /** 将一个字符串输出到浏览器 */ protected void writeJson(String json) { PrintWriter pw = null; try ...

  4. form表单序列化数据之后,追加额外数据

    form表单序列化数据之后追加额外数据多使用在js中,下面是追加额外数据的代码: <span style="font-size:18px;">$.param({'inv ...

  5. 防止表单提交时刷新页面-阻止form表单的默认提交行为

    最近在写 ajax 提交的时候遇到一个问题,在执行 ajax 提交之后,浏览器页面自动刷新了,主要是没有 由于form 表单的默认提交行为.一下是几种阻止 form 表单默认提交行为的方式. 1.使用 ...

  6. C# 模拟提交 Form表单的数据

    用 HttpWebRequest Post方法模拟提交Form表单数据时,需要设置 ContentType 为 "application/x-www-form-urlencoded" ...

  7. Laravel Form 表单的数据校验

    例如,要使用手机号加验证码的方式提供登录网站的功能,那么在处理前端提交的 form 表单时,就不得不对提交的手机号及验证码做基本的数据校验. 手写规则,非常浪费时间.使用 laravel 内置的 va ...

  8. 浏览器原生 form 表单POST 数据的两种方式

    我们在提交表单的时候,form表单参数中会有一个enctype的参数.enctype指定了HTTP请求的Content-Type. 常用有两种:application/x-www-form-urlen ...

  9. JS form 表单收集 数据 formSerialize

    做后台系统的时候通常会用到form表单来做数据采集:每次一个字段一个字段的去收集就会很麻烦,网站也有form.js插件可以进行表单收集,并封装成一个对象,通过ajax方法传到后台:现在介绍一种直觉采集 ...

随机推荐

  1. 运行 composer update,提示 Allowed memory size of bytes exhausted

    composer update运行之后,提示 PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to ...

  2. springboot中pageHelper插件 list设置不进去 为null

    分页pageHelper中list放不进去值  为null,可能的解决方案如下: 1. 注意代码顺序,PageHelper.startPage(pageNumber,pageSize)要放在查询Lis ...

  3. 廖雪峰Java12maven基础-1maven入门-3构建流程

    maven是一个Java项目管理和构建工具: 标准化项目结构 标准化构建流程(编译.打包.发布) 依赖管理 Maven的构建流程 clean 删除所有编译生成的文件 compile 编译源码.编译测试 ...

  4. Duilib入门文档提供下载

    版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] Duilib入门文档 基本框架 编写界面xml 响应事件 贴图描述 类html文本描述 动态换肤 Dll插件 资源打包 Duil ...

  5. python事件调度库sched

    事件调度 sched模块内容很简单,只定义了一个类.它用来最为一个通用的事件调度模块. class sched.scheduler(timefunc, delayfunc)这个类定义了调度事件的通用接 ...

  6. Elasticsearch基本命令

    检查集群运行情况:    GET ->   localhost:9200/_cat/health?v 查看集群节点列表:    GET ->   localhost:9200/_cat?n ...

  7. MySQL 中LIMIT的使用详解

    在使用数据库过程中,常会遇到查询或者导出某个数据表或者查询集的前几条或者后几条记录,LIMIT可以很好的满足需求. LIMIT基本语法: 如果只给定一个参数,表示记录数. mysql; ) 相当于 m ...

  8. Redis深度历险——核心原理与应用实践

    高可用架构」的各位老铁们,你们好!你是否还记得上个月发布的文章中,有两篇深入讲解Redis的文章,分别是和,广大粉丝读者们对这两篇文章整体评价颇高.而我就是这两篇文章的原创作者「老钱」(钱文品),我是 ...

  9. Mui本地打包笔记(一)使用AndroidStudio运行项目 转载 https://blog.csdn.net/baidu_32377671/article/details/79632411

    转载 https://blog.csdn.net/baidu_32377671/article/details/79632411 使用AndroidStudio运行HBuilder本地打包的Mui项目 ...

  10. PAT甲级——A1094 The Largest Generation

    A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...